不吃鱼

导航

 

官方文档http://nginx.org/en/docs/

nginx模块:

基本状态信息:ngx_http_stub_status_module,stub_status

目录中选择一个随机主页:ngx_http_random_index_module,random_index,隐藏文件不会读取到,mv 3.html .3.html

http内容替换:ngx_http_sub_module,sub_filter、sub_filter_once

限制连接频率:ngx_http_limit_conn_module,limit_conn、limit_conn_zone

限制请求频率:ngx_http_limit_req_module,limit_req、limit_req_zone

ab -n 50 -c 20 http://localhost/1.html;发起50个请求,并发为20,测试限制;

基于IP的访问控制:ngx_http_access_module,allow、deny

http_x_forwarded_for=Client IP, Proxy(1) IP,Proxy(2) IP, ...

基于用户的信任登录:ngx_http_auth_basic_module,auth_basic、auth_basic_user_file

生成name1:password1格式文件工具:yum install httpd-tools -y;htpasswd -c ./auth_conf jeson

大文件分片请求:ngx_http_slice_module,slice

静态资源web服务

核心模块:ngx_http_core_module,sendfile、tcp_nopush(提高网络包的传输效率)、tcp_nodelay(提高网络包的传输实时性)

gzip模块:ngx_http_gzip_module,gzip(压缩传输)、gzip_comp_level、gzip_http_version

预读gzip功能:ngx_http_gzip_static_module

应用支持gunzip的压缩:ngx_http_gunzip_module

配置expires

添加Cache-Control、Expires头

Syntax:expires [modified] time;

    expires epoch | max | off;

Default:expires off;

Context:http,server,location,if in location

跨域访问:ngx_http_headers_module,add_header

  • 浏览器跨域的解决方式有很多种:
  • 1.jsonp 需要目标服务器配合一个callback函数。
  • 2.window.name+iframe 需要目标服务器响应window.name。
  • 3.window.location.hash+iframe 同样需要目标服务器作处理。
  • 4.html5的postMessage+ifrme这个也是需要目标服务器或者说是目标页面写一个postMessage,主要侧重于前端通讯。
  • 5.CORS需要服务器设置header:Access-Control-Allow-Origin。
  • 6.nginx反向代理 这个方法一般很少有人提及,但是他可以不用目标服务器配合,不过需要你搭建一个中转nginx服务器,用于转发请求。

防盗链:ngx_http_referer_module,valid_referers

curl -e http://请求地址 -I http://资源,curl操作资源指向访问

代理服务

正向代理:代理对象是客户端

FQ代理

反向代理:代理对象是服务端

代理模块:ngx_http_proxy_module,proxy_pass

负载均衡

禁用8082端口:iptables -I INPUT -p tcp --dport 8082 -j DROP

upstream模块:ngx_http_upstream_module,upstream、ip_hash、hash

后端服务器在负载均衡调度中的状态:

down 当前server不参与负载均衡

backup 预留的备份server

max_fails 允许请求失败的次数

fail_timeout 经过max_fails后,server暂停时间

max_conns 限制最大的接收连接数

调度算法

轮询 按时间逐一分配server

加权轮询 weight越大分配几率越大

ip_hash ip的hash值分发

least_conn 最少连接数,哪个server少就分发

url_hash url的hash值分发

hash关键数值 hash自定义的key

 缓存

代理模块:ngx_http_proxy_module,proxy_cache、proxy_cache_path、proxy_cache_valid、proxy_cache_key、proxy_no_cache

清除cache:

rm -rf 缓存路径

第三方扩展模块ngx_cache_purge

*支持Websocket

location /websockt {
        proxy_pass     http://web;
        #############添加对websocket的配置支持###
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
}

*多层代理IP透传

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

配置SSL证书

甲方提供证书和密钥文件
一般会给我们两个文件,crt证书文件和key私钥文件。将这两个文件上传至/usr/local/nginx/conf目录下,给nginx配置ssl只需在响应的server节点中配置如下属性即可。
listen  443 ssl;
server_name  域名;   #证书域名,必须配置且与申请时一致
ssl on;
ssl_certificate nginx.crt;     #证书文件
ssl_certificate_key nginx.key;     #证书秘钥文件
我方申请证书

在向CA公司申请时可能CA公司会需要证书申请文件,生成方法。

[root@localhost ~]# openssl genrsa -out nginx.key 2048 #会生成nginx.key文件即服务器秘钥文件,请妥善保管
[root@localhost ~]# openssl req -new -key nginx.key -out nginx.csr #该步骤会生成nginx.csr文件及证书申请文件,请妥善保管
[root@localhost ~]# openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.crt   #将证书请求文件生成证书文件

第三方模块

1、后端服务健康检查:nginx_upstream_check_module,check 

upstream cluster {
    # simple round-robin
    server ip:port;
    server ip:port;
    check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
如上配置即可实现针对 upstream 中每一个 server 执行健康检查操作,每个三秒钟对后端发起一次检测,检测后端的 80 端口,如果后端在 1s 内不给出响应,连续5次出现不响应的情况,则将该机器踢出可用服务器列表中,如果后期连续检测两次该服务器正确的响应了,则重新将该服务器调度至服务器可用列表中。
 
若希望采用 http 的检测方式,可以使用如下配置
upstream cluster {
    # simple round-robin
    server ip:port;
    server ip:port;
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    check_http_send "GET /intelliq-web/index/home HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}
其中在 check_http_send 中配置的 GET /intelliq-web/index/home HTTP/1.0\r\n\r\n ,表示在检查时向后端发送 /intelliq-web/index/home 请求,以此判断后端是否正常,如果后端返回 2xx 和 3xx 则表示是存活的,请注意在配置时请注意 HTTP/1.0\r\n\r\n ,该值千万不要配置错误。

 

posted on 2020-04-30 15:30  不chi鱼  阅读(139)  评论(0编辑  收藏  举报