服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx timeout超时配置详解

Nginx timeout超时配置详解

2020-03-19 14:57梦想272 Nginx

这篇文章主要介绍了Nginx timeout超时配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

最近项目中用到了nginx,后台用的是Java, 发现有一个请求后台处理操过了1分钟,结果请求Status Code为504 Gateway Time-out.

理解了下nginx 所有timeout相关的配置,如下:

keepalive_timeout

HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

http keep-alive, 網頁的每一個請求都是HTTP (圖片, CSS等), 而打開HTTP 請求是要先建立TCP 連接, 而如果一個頁面每個請求都要打開及關閉一個TCP 連接就會做成資源的浪費. keepalive_timeout 就是當一個HTTP 請求完成, 其TCP 連接會存留下來的時間, 如果這時有另一個HTTP 請求過來, 會複用這個TCP 連接, 如果再沒有新的請求過來, 才會關閉其TCP連接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;

  • send_timeout :发送数据至客户端超时, 默认60s, 如果连续的60s内客户端没有收到1个字节, 连接关闭
  • proxy_connect_timeout: nginx与upstream server的连接超时时间
  • proxy_read_timeout: nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭
  • proxy_send_timeout: nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭

so_timeout:

當用戶跟SERVER開啟了TCP CONNECTION --> 一段長時間這個CONNECTION 沒traffic (so_keepalive timeout) --> SERVER 發出探測包看用戶是否還存在 --> 若探測包沒回, 則關閉TCP CONNECTION 

?
1
so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
?
1
2
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没有数据报文发送探测报文)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/chunzhiyan/article/details/103601912

延伸 · 阅读

精彩推荐