Details how to configure timeout on the Nginx server

  • 2020-05-10 23:28:11
  • OfStack

1. When you need it

            is used to set the time for the requested resource and the return of the server, ensuring that one request takes a fixed time, beyond which 504 timeout is reported! This ensures that one request takes too long.

2. Main parameters


          nginx server nginx server           nginx server

                fastcgi_connect_timeout 75;   link

                fastcgi_read_timeout 600;     read

                fastcgi_send_timeout 600;     sends the request

       .
              fastcgi_read_timeout refers to the timeout period for the entire fastcgi process to send response to the nginx process
              fastcgi_send_timeout refers to the timeout period for the entire nginx process to send request to the fastcgi process

        both of these options default to seconds (s) and can be manually specified as minutes (m), hours (h), etc


3. Other common parameters and parameter description


          keepalive_timeout   600; The     connection timeout, 1 minute, can be set depending on how long the request (for example, background import) takes

              proxy_connect_timeout 600;       1 minute

              proxy_read_timeout 600;       1 minute

nginx timeout configuration parameter description:

keepalive_timeout

Syntax keepalive_timeout timeout [header_timeout]

The default value 75 s

Context http server location

Note that the first parameter specifies the keep-alive connection timeout with client. The server will close the connection after this time. The optional second parameter specifies the time value in the response header Keep-Alive: timeout=time. This header allows some browsers to actively close the connection so that the server does not have to close the connection. Without this parameter, nginx does not send the Keep-Alive response header (although it is not the header that determines whether the connection is "keep-alive")

The values of the two parameters may not be the same

Notice how different browsers handle the "keep-alive" header

MSIE and Opera ignore "Keep-Alive: timeout= < N > " header.

MSIE hold the connection for about 60-65 seconds, then send TCP RST

Opera permanently maintains long connections

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

Konqueror maintains long connection N seconds

proxy_connect_timeout

Grammar proxy_connect_timeout time

The default value of 60 s

Context http server location

Note that this directive sets the connection timeout with upstream server, and it is important to remember that this timeout cannot exceed 75 seconds.

This is not the time to wait for the back end to return to the page, which is declared by proxy_read_timeout. If your upstream server is up, but hanging is live (for example, there are not enough threads to process the request, so put your request in the request pool for later processing), then this declaration is useless, as the connection to the upstream server has already been established.

proxy_read_timeout

Grammar proxy_read_timeout time

The default value of 60 s

Context http server location

Explains that this directive sets the read timeout with the proxy server. It determines how long nginx will wait to get a response to a request. This time is not the time to obtain the entire response, but the time for the two reading operations.

client_header_timeout

Grammar client_header_timeout time

The default value of 60 s

Context http server

Instructions specify the timeout time to wait for client to send 1 request header (e.g., GET/HTTP/1.1). A timeout is only considered if no request header is received in read once. If client does not send anything during the timeout period, nginx returns HTTP status code 408(" Request timed out ")

client_body_timeout

Grammar client_body_timeout time

The default value of 60 s

Context http server location

Explains that this directive sets the read timeout for the request body (request body). It is set to timeout only if the body of the request is not received once in readstep. After a timeout, nginx returns the HTTP status code 408(" Request timed out ")

lingering_timeout

Grammar lingering_timeout time

The default value of 5 s

Context http server location

Note: after lingering_close takes effect, before closing the connection, it will detect whether the data sent by the user has arrived at the server. If there is no data readable after the time of lingering_timeout, it will directly close the connection. Otherwise, the connection must be closed after the data on the connection buffer has been read and discarded.

resolver_timeout

Grammar resolver_timeout time

30 s default values

Context http server location

This directive sets the DNS resolution timeout

proxy_send_timeout

Grammar proxy_send_timeout time

The default value of 60 s

Context http server location

Note that this specification sets the timeout time for sending the request to the upstream server. The timeout is not set for the entire send period, but for the two write operations. If upstream does not receive new data after the timeout, nginx closes the connection

proxy_upstream_fail_timeout (fail_timeout)

Syntax server address [fail_timeout=30s]

The default value of 10 s

Context upstream

Note the parameters of the server directive under the Upstream module, and the unoperable time of the upstream backend after a specified number of failures (max_fails) is set is 10 seconds by default

Example 4.

Here is an example of an upregned timeout for Nginx.
See if the time meets the requirement. The three parameters in nginx.config:

fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;

The units above are seconds.

If you are using an Nginx proxy, you can add:


proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;

To:


location /foo {
   proxy_pass http://xxx.xxx.xxx.xxx:8080/foo;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_connect_timeout 300s;
   proxy_send_timeout 300s;
   proxy_read_timeout 300s;
   access_log /var/log/nginx/access.foo.log main;
   error_log /var/log/nginx/error.foo.log;
}

Related articles: