nginx(二)服务器中配置超时时间的方法

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第35天,点击查看活动详情

访问超时这个事一般都是在nginx中配置的。

之前大脑发昏,一直研究PHP来着,后来发现,不行,PHP超时怎么配都不好用。

首先说一下配置的位置,是在每个域名的配置文件中配置的

我的域名配置文件如下:

server {
        listen       443 ssl;
        server_name  xxx.xxxx.net;
        ssl_certificate     /xxx/xxx/xxx/xxx/xxxx_xxx.xxx.net.pem;
        ssl_certificate_key  /xxx/xxx/xxx/xxx/xxxx_xxx.xxx.net.key;
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        root   /xxx/xxx/xxx/xxx/xxxx/;
        location / {
 
            try_files $uri $uri/ /index.php?$query_string;
            index  index.html index.htm;
        }
        location ~ .php(.*)$  {
            fastcgi_pass   127.0.0.1:9000;
 
            #配置访问超时时间
            keepalive_timeout 60000s;
            fastcgi_connect_timeout 36000s;
            fastcgi_send_timeout 36000s;
            fastcgi_read_timeout 36000s;
 
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
 
    }
    server {
       listen 80;
       server_name  dev.mools.net;
       rewrite ^(.*)$  https://$host$1 permanent;     #将该域名的http访问重写至https
    }

配置访问超时的代码其实就是下面的四行:

            #配置访问超时时间
            keepalive_timeout 60000s;
            fastcgi_connect_timeout 36000s;#链接
            fastcgi_send_timeout 36000s;#发请求
            fastcgi_read_timeout 36000s;#读取

下边我们大概来看下,基本上其对应的都是什么意思:

Fastcgi_connect_timeout

是指fastcgi进程想nginx进行发送请求保持的连接的时间。

fastcgi_read_timeout

是指fastcgi进程向nginx进程发送response的整个过程的超时时间

fastcgi_send_timeout

是指nginx进程向fastcgi进程发送request的整个过程的超时时间

keepalive_timeout (长连接类型)

语法 keepalive_timeout timeout [ header_timeout ]
默认值 75s
上下文 http server location
说明 第一个参数指定了与client的keep-alive连接超时时间。服务器将会在这个时间后关闭连接。可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。没有这个参数,nginx不会发送Keep-Alive响应头(尽管并不是由这个头来决定连接是否“keep-alive”)(服务器在返回数据给用户时,在头header文件中会添加keepalive字段,75s,浏览器在这个时间后能够主动关闭连接)
两个参数的值可并不相同

注意不同浏览器怎么处理“keep-alive”头

MSIE和Opera忽略掉"Keep-Alive: timeout=" header.

MSIE保持连接大约60-65秒,然后发送TCP RST

Opera永久保持长连接

Mozilla keeps the connection alive for N plus about 1-10 seconds.

Konqueror保持长连接N秒

其他的一些参数:

1 :proxy_connect_timeout

语法 proxy_connect_timeout time

默认值 60s

上下文 http server location

说明 该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。

这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

2 :proxy_read_timeout

语法 proxy_read_timeout time

默认值 60s

上下文 http server location

说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

3 :client_header_timeout

语法 client_header_timeout time

默认值 60s

上下文 http server

说明 指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

4 :client_body_timeout

语法 client_body_timeout time

默认值 60s

上下文 http server location

说明 该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

5 :lingering_timeout

语法 lingering_timeout time

默认值 5s

上下文 http server location

说明 lingering_close生效后,在关闭连接前,会检测是否有用户发送的数据到达服务器,如果超过lingering_timeout时间后还没有数据可读,就直接关闭连接;否则,必须在读取完连接缓冲区上的数据并丢弃掉后才会关闭连接。

6 :resolver_timeout

语法 resolver_timeout time

默认值 30s

上下文 http server location

说明 该指令设置DNS解析超时时间

7 :proxy_send_timeout

语法 proxy_send_timeout time

默认值 60s

上下文 http server location

说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

8 :proxy_upstream_fail_timeout(fail_timeout)

语法 server address [fail_timeout=30s]

默认值 10s

上下文 upstream

说明 Upstream模块下 server指令的参数,设置了某一个upstream后端失败了指定次数(max_fails)后,该后端不可操作的时间,默认为10秒

以上大概就是nginx配置访问超时的所有内容。

有好的建议,请在下方输入你的评论。

欢迎访问个人博客 guanchao.site

欢迎访问我的小程序:打开微信->发现->小程序->搜索“时间里的”

猜你喜欢

转载自juejin.im/post/7114469621444378632