首页  

nginx常用优化     所属分类 nginx 浏览量 787
sendfile on; 
tcp_nopush on; 
tcp_nodelay on;

硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >> 协议栈
sendfile
硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈

nginx 负载均衡 反向代理
长连接
client到nginx  长连接
nginx到server  长连接

与client的长连接
http {
    keepalive_timeout  120s 120s;
    keepalive_requests 10000;
}
keepalive_timeout timeout [header_timeout];
keepalive_timeout默认75s
第一个时间
第二个时间  响应header 设置 Keep-Alive: timeout=xxx

keepalive_requests
设置一个keep-alive连接上可以服务的请求的最大数量,超出改次数时,关闭连接  默认 100


nginx和后端server (upstream) 长连接
nginx访问后端 默认 短连接 HTTP1.0

http {
    upstream  BACKEND {
        server   192.168.0.1:8080  weight=1 max_fails=2 fail_timeout=30s;
        server   192.168.0.2:8080  weight=1 max_fails=2 fail_timeout=30s;
        // 长连接 连接池个数
        keepalive 300;       
    }
server {
        listen 8080 default_server;
        server_name "";
        location /  {
            proxy_pass http://BACKEND;
            proxy_set_header Host  $Host;
            proxy_set_header x-forwarded-for $remote_addr;
            proxy_set_header X-Real-IP $remote_addr;
            add_header Cache-Control no-store;
            add_header Pragma  no-cache;
            // 这两个最好也设置
            proxy_http_version 1.1;        
            proxy_set_header Connection "";
        }
    }
}

location中有两个参数需要设置
http {
    server {
        location /  {
            // 这两个最好也设置
            proxy_http_version 1.1; 
            proxy_set_header Connection "";
        }
    }
}
HTTP协议 长连接支持 1.1版本 ,最好通过proxy_http_version指令设置为 1.1
proxy_set_header Connection "";
清理从client过来的http header
client和nginx之间是短连接,nginx和upstream之间也可以开启长连接
这种情况下必须清理来自client请求中的 Connection header

upstream中的keepalive
The connections parameter sets the maximum number of idle keepalive connections to upstream servers connections
设置到upstream服务器的空闲keepalive连接的最大数量

When this number is exceeded, the least recently used connections are closed. 
当这个数量被突破时,最近使用最少的连接将被关闭

It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open.
keepalive指令不会限制一个nginx worker进程到upstream服务器连接的总数量

上一篇     下一篇
linux 获取进程工作目录

日志框架关键组件

Log4j2 lookup 安全漏洞

JFR实战

log4j2使用要点

JNDI demo