时间:2020-11-05 17:59:21 | 栏目:Nginx | 点击:次
最近项目中用到了nginx,后台用的是Java, 发现有一个请求后台处理操过了1分钟,结果请求Status Code为504 Gateway Time-out.
理解了下nginx 所有timeout相关的配置,如下:
keepalive_timeout
HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
http keep-alive, ?W?的每一???求都是HTTP (?D片, CSS等), 而打?_HTTP ?求是要先建立TCP ?B接, 而如果一???面每???求都要打?_及?P?]一??TCP ?B接就??做成?Y源的浪?M. keepalive_timeout 就是??一??HTTP ?求完成, 其TCP ?B接??存留下?淼?r?g, 如果?@?r有另一??HTTP ?求?^??, ???}用?@??TCP ?B接, 如果再?]有新的?求?^??, 才???P?]其TCP?B接
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; client_max_body_size 8192m; #gzip on; #include /etc/nginx/conf.d/*.conf; server { listen 80 so_keepalive=30m::; listen 443 default ssl; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/portalkey.key; #ssl_password_file /etc/nginx/ssl/ssl.pass; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_request_buffering off; proxy_pass http://127.0.0.1:8011/; proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; send_timeout 180; } location /test1_url/ { proxy_pass http://127.0.0.1:8008/; proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; send_timeout 180; } location /test2_url/ { proxy_pass http://127.0.0.1:3000/; proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; send_timeout 180; } } }
# 配置段: http,默认75s
keepalive_timeout 60;
so_timeout:
??用?舾?SERVER?_?⒘?TCP CONNECTION --> 一段?L?r?g?@??CONNECTION ?]traffic (so_keepalive timeout) --> SERVER ?l出探?y包看用?羰欠襁?存在 --> 若探?y包?]回, ?t?P?]TCP CONNECTION
so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
so_keepalive=30m::10 will set the idle timeout (TCP_KEEPIDLE) to 30 minutes, leave the probe interval (TCP_KEEPINTVL) at its system default, and set the probes count (TCP_KEEPCNT) to 10 probes.
以上三个参数只能使用一个,不能同时使用, 比如so_keepalive=on, so_keepalive=off或者so_keepalive=30s::(表示等待30s没有数据报文发送探测报文)