背景
客户端发起请求 通过slb 给到nginx,然后nginx再给到后端,经常出现请求接口超时,nginx日志都没有问题(也就是给到nginx处理的请求都完成了),查看slb的流量,远远小于带宽(网络层面应该没有问题),所以问题应该在请求到达nginx操作系统层面。优化nginx在操作系统上面的请求。
worker_processes 8; 改为核心数
events {
use epoll;
worker_connections 65535;
multi_accept on; #可以一次建立多个连接
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_connect_timeout 600;
client_max_body_size 500m;
vim /etc/sysctl.conf
vm.swappiness = 0 kernel.sysrq = 1 net.ipv4.neigh.default.gc_stale_time = 120 # see details in https://help.aliyun.com/knowledge_detail/39428.html net.ipv4.conf.all.rp_filter = 0 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.all.arp_announce = 2 # see details in https://help.aliyun.com/knowledge_detail/41334.html net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_synack_retries = 2 net.ipv6.conf.lo.disable_ipv6 = 1 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.somaxconn = 40960 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_keepalive_time = 150 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_max_tw_buckets = 20000 net.ipv4.tcp_max_syn_backlog = 8096 net.ipv4.ip_local_port_range = 1024 65000
下面的命令 可能和上面的效果一样,直接运行
echo "0" > /proc/sys/net/ipv4/tcp_tw_recycle echo "0" > /proc/sys/net/ipv4/cp_timestamps sysctl -w net.ipv4.tcp_tw_recycle=0 sysctl -w net.ipv4.tcp_timestamps=0 net.ipv4.tcp_tw_recycle = 0
ulimit -a 这个也调整一下
cat wxpt.access.log-20210518 |grep jianhuochakucun |grep -v "ups_status:200"
location /oms_order {
proxy_pass http://omsorder/;
proxy_redirect default;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
client_max_body_size 50m;
proxy_buffer_size 16k;
proxy_buffering on;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
proxy_ignore_client_abort off;
当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后nginx也会中断客户端请求并立即记录499日志,默认为off