How to increase the requests timeout on Nginx

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. Nginx is one of the most used web servers in the world along with Apache. So it is possible to find a lot of tricks on the internet to help us with that. In this post, I will show you a very convenient and useful one like increasing the timeout of the requests on Nginx. This will allow you more flexibility when configuring Nginx and adapting it to the needs of the various applications or websites you have.

Speed is almost everything on the Internet

When a user surfs the internet they want to do it as fast as possible. In this sense, nginx is one of the fastest web servers out there, which is why many websites prefer it to Apache.

Another reason why many people prefer Nginx is because of the flexibility it gives us when it comes to configuration. Not that Apache is not, but in Nginx, we can configure almost anything and with much more flexibility…

One of the things we can configure in Nginx is the timeout of the requests that are made to each site on the server. Knowing how to manage this is vital to control the access to these applications implying a better use of network resources and ultimately affecting the loading speed.

So the Internet is not all about speed, but it does have a big influence on the user experience.

How to increase the requests timeout on Nginx

By default, requests timeout is 60 seconds. In some cases, it is necessary to increase it to respond to long requests like database queries and so on. The catch is that if the timeout is exceeded, the NGINX server will return a 504 error.

Usually, we have several VirtualHosts in the system, so you have to modify the configuration file of each one of them. These configuration files are located in the /etc/nginx/sites-available folder.

And the values we have to modify are proxy_read_timeout, proxy_connect_timeout and proxy_send_timeout inside the server directive.

For example

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

In this case, the value is expressed in seconds, so by defining 300 we are talking about 5 minutes of wait time.

Then save your changes and close the editor.

It is advisable to first check if the syntax is correct to avoid errors and frustrations.

nginx -t

To apply the changes you have to restart Nginx.

systemctl restart nginx

Also, we will be able to increase the time for all the projects and sites we have in the system. This can be done by editing the main Nginx configuration file.

sudo nano /etc/nginx/nginx.conf

And inside the http directive make the modifications as in this example:

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

Save the changes and close the editor. And apply the changes by restarting Nginx. In this case, the changes will affect all virtualhosts.

How to increase the requests timeout in a specific route

This option is interesting when we don’t want to increase the timeout in the whole site but some specific routes. For example, very useful for database queries.

In this case, in the virtual host configuration file set a new section indicating the route and the parameters

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

In this way and so simple we will be able to realize the objective

Conclusion

In this post, you learned how to configure Nginx to increase the request time. So, enjoy it

Nginx Documentation

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook