Three Examples to Increase Request Timeout in NGINX

NGINX is one of the best choices for serving long-running requests because it has a robust architecture. However, you might need to increase your request timeout to ensure that NGINX can easily handle these heavy loads.

By default, it is set at 60 seconds, but if you cannot serve your entire traffic without hitting this limit, try raising it higher, like 75 or 100 milliseconds (or whatever makes sense for how busy the site is). The error “504: Gateway Timeout” will appear if there are not enough resources available on the host machine; however, using some proxy timeout settings should fix these problems – including read/connect delays as well as sending messages alone; before waiting till everyone sends them back!

In the following tutorial, you will learn how to increase request timeout in NGINX using proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives to fix 504 Gateway Timeout error.

Open NGINX Configuration File

Linux servers host most Nginx web applications installations for most users, and you will need to open your Nginx configuration block “nginx.conf”.

Some popular text editors and opening commands are as follows.


sudo nano /etc/nginx/nginx.conf
sudo vi /etc/nginx/nginx.conf
sudo vim /etc/nginx/nginx.conf

The standard location of Nginx installations is /etc/nginx. Still, installations or configuration files can be found /usr/local/nginx/conf and /usr/local/etc/nginx depending on the distribution/operating system.

Increase Request Timeout in NGINX

Next, you will need to increase request timeout by adding proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives to http or server block.

Most tutorials recommend 300 seconds, but you should increase as minimal as possible. What other tutorials do not teach setting a high timeout can let attackers DDOS you, start at the double the default, which is 60 seconds, so you will start at 120 seconds and increase another 60 after that going in 60, 120, 180, 240, 320, 380 and so on until you find the best spot.

Example:

http {
   ...
   proxy_read_timeout 120;
   proxy_connect_timeout 120;
   proxy_send_timeout 120;
   ...
}

In the above example, you are setting in the HTTP { section to automatically enable an increased timeout across all servers running off your Nginx installation.

Alternatively, if you want to increase request timeout for a specific server or subdomain, then add proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives for its server block.


server {
   ...
   proxy_read_timeout 120;
   proxy_connect_timeout 120;
   proxy_send_timeout 120; 
   ...
}

Further fine-tuning can be accomplished if you want to increase request timeout for a specific path, then add proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives for its location / in your server block.

location /example-path {
   ...
   proxy_read_timeout 120;
   proxy_connect_timeout 120;
   proxy_send_timeout 120; 
   ...
}

Restart NGINX Service

Before restarting your Nginx service to make the changes live, you will need to verify the syntax of your newly added timeout settings will not cause an error.

In your terminal, run the following command.

sudo nginx -t

The result, unless implemented wrong, should be ok; next, restart your service.

sudo systemctl restart nginx

Comments and Conclusion

You should now have a better understanding of how to increase your request timeout in NGINX. Remember that it is crucial to test any changes you make to ensure they do not impact your website’s performance. If you need help configuring NGINX or troubleshooting any issues, please feel free to reach out to us for assistance. Thanks for reading!


Not what you were looking for? Try searching for additional tutorials.

Leave a Comment