How to Increase Request Timeout in NGINX

NGINX is a popular web server used by many high-traffic websites and applications. It supports very extreme server loads using very few resources. This results in a very fast user experience. But sometimes, a user may receive request timeout while trying to load a page. There are can be several reasons for this. In this article, we will learn about the different types of request timeouts, and how to increase request timeout in NGINX.

Types of Request Timeout in NGINX

There are mainly 3 types of request timeouts that users may come across while using NGINX server. They are:

504 Gateway Timeout Error

This is the most common request timeout response you get while using NGINX. It happens when NGINX is set up as a proxy server and has to keep waiting for the response from an upstream server, such as a database or application server. This occurs due to server reasons such as the upstream server is down or overloaded, slow network connect between NGINX and upstream server, or faulty NGINX configuration.

408 Request Timeout

You get this type of request timeout response when NGINX does not receive a complete request from the client within a specific time limit. This error occurs mainly due to a slow client, and not NGINX server. It happens because of network connection issues between the client and server, or if the request itself gets timed out on client side. It may also be due to incorrect NGINX configuration.

413 Request Too Large

NGINX returns ‘413 Request Entity Too Large’ response in case the client request size is much bigger than the limit allowed on NGINX Server. You will see this problem mostly in case of file uploads, if you try to upload a large file.

Different Timeout Directives in NGINX

There are several timeout directives supported by NGINX server, each meant for a specific purpose. You can increase request timeout by simply setting their values as per your requirement. Here are the most common ones.

  • keepalive_timeout – max time for a client connection to be kept on the server
  • send_timeout – max time to send a response back to a client
  • proxy_connect_timeout – max time to connect to proxied server
  • proxy_read_timeout – max time to read a single proxied server response
  • proxy_send_timeout – max time to send a single proxied server request
  • client_body_timeout – max time to read the body of a single client request
  • client_header_timeout – max time to read the header of a single client request
  • fastcgi_connect_timeout – max time to connect to FastCGI server
  • fastcgi_read_timeout – max time to read a single FastCGI server response
  • fastcgi_send_timeout – max time to send a single FastCGI server request

Depending on your NGINX configuration, and the type of request timeout, you need to use the appropriate directive. Each of these directives has a default timeout value of 60 seconds. They accept multiple time dimensions such as s for seconds, d for days, and so on. Here is an example to set send_timeout directive to 100 seconds.

send_timeout 100s

The above directives work for 504 and 408 error response codes. But if your users get ‘413: Request Entity Too Large’ then you need to increase the max request body size accepted by your server. For this purpose, you need to use the client_max_body_size directive. Here is an example to set it to 10Mb size.

client_max_body_size 10M;

How to Use Timeout Directives

Each of the above mentioned directives can be included in main http block, any of the server blocks or location blocks.

Here are different examples to set request timeout to 300 seconds. When you include the directive in an http block, it will be applicable to all servers that are running in this http block. This is useful if you want to consistently set timeout values across multiple domains, sub domains and apps.

http{
...
send_timeout 300s;
...
}

When you add the directive in a server block, it will be applied only to that specific block and not the others. But it will be applicable to all URLs served by that server block. This is useful if you wish to have separate timeout values for each website/domain or subdomain.

server{
...
send_timeout 300s;
...
}

When you include the directive in a location block, then it will be applied only to all URLs served by that location block and not the other location blocks in that server. This allows you to precisely increase the request timeouts as required.

location / {
...
send_timeout 300s;
...
}

Steps to Increase Request Timeout in NGINX

By default, the request timeout value is 60 seconds. Here are the steps to increase request timeout in NGINX to 300 seconds.

1. Open NGINX Configuration File

First, we need to open NGINX configuration file nginx.conf in vi editor. It is generally located at /etc/nginx or /usr/local/etc/nginx

sudo vi /etc/nginx/nginx.conf

2. Add or Modify Timeout Directives

Let us say you have setup NGINX as a proxy and want to increase request timeout for all websites & apps hosted on your NGINX. In that case, we add the following send_timeout directive to http block.

http {
...
send_timeout 300s;
...
}

Depending on your requirement, you can use any of the timeout directives that we have described earlier. Sometimes, you may need to use more than one timeout directives. If you want to completely disable the timeout then set it to a large value like 1 day or so, as shown. But please note, doing so will increase resource consumption of your server.

send_timeout 1d;

Save and close the file.

3. Test Configuration

Run the following command to test configuration.

sudo nginx -t

4. Restart Server

Run the following command to restart NGINX server and apply changes.

sudo systemctl restart nginx

Conclusion

Users may get timed out due to several reasons such as network issues, server overload or faulty configuration. In this article, we have learnt several simple ways to increase request timeout in NGINX server. It is very useful to prevent your users from getting timed out, due to NGINX configuration. If your website or application uses long running processes, such as in case of data analytics & reporting, then you may need to increase request timeout value for your server. But please note, if you increase request timeout for your server, then it will have to serve more connections at the same time, since they will continue to exist longer. This means your server will consume more resources. So there needs to be a balance between your server’s request timeout value and the resources it consumes.

Also read:
How to Uninstall NGINX Server
How to Password Protect Directory in NGINX
NGINX Restrict Access to Directory

Leave a Reply

Your email address will not be published. Required fields are marked *