Set Nginx Timeout: Proxy Configuration Guide

In Nginx, you can set interface timeout using proxy_connect_timeout and proxy_read_timeout.

The proxy_connect_timeout is used to set the timeout for establishing a connection with the backend server, in other words, the connection timeout.

The proxy_read_timeout is used to set the timeout for receiving responses from the backend server, specifically the timeout for reading responses.

It is possible to set timeout time for a specific location or upstream in Nginx configuration file. For example:

location /api {
    proxy_pass http://backend_server;
    proxy_connect_timeout 5s;
    proxy_read_timeout 10s;
}

In the example above, when accessing the /api interface, Nginx will forward the request to the backend_server, with a connection timeout of 5 seconds and a response read timeout of 10 seconds.

It is important to note that the connection timeout only takes effect when establishing the connection, while the read response timeout starts to count after the connection has been established.

In addition, if the backend server does not respond within the timeout period, Nginx will return a 504 Gateway Timeout error to the client. Using proxy_next_upstream can help redirect to other backend servers to avoid a single point of failure.

bannerAds