Increase or set request timeout in nginx; Through this tutorial, we will learn how to increase or set request timeout in Nginx.
By default, the NGINX request timeout is set to 60 seconds. Sometimes we need to increase the request timeout in NGINX to handle long running requests. NGINX will give a “504: Gateway Timeout” error if we do not increase the request timeout value. Here’s how to increase request timeout in NGINX using proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives to fix 504 gateway timeout error.
How to Increase Request Timeout in NGINX
Just follow the following steps to increase request timeout in NGINX:
- 1 – Open NGINX configuration file
- 2 – Increase Request Timeout in NGINX
- 3 – Restart NGINX
1 – Open NGINX configuration file
First of all, Open the terminal and execute the following command on command line to open NGINX configuration file in a text editor:
sudo vi /etc/nginx/nginx.conf
Important Note:- NGINX file may be located at /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx depending on your installation.
2 – Increase Request Timeout in NGINX
To increase request timeout to 300 seconds, then add proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives to http or server block
http{ ... proxy_read_timeout 300; proxy_connect_timeout 300; proxy_send_timeout 300; ... }
To increase request timeout only 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 300; proxy_connect_timeout 300; proxy_send_timeout 300; ... }
To increase request timeout only for specific folder or URL, then add proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout directives for that specific location block.
location /upload { ... proxy_read_timeout 300; proxy_connect_timeout 300; proxy_send_timeout 300; ... }
3 – Restart NGINX
Then, execute the following command on the command line to restart Nginx server:
sudo nginx -t
If there are no errors, so execute the following command to restart NGINX server:
sudo service nginx reload #debian/ubuntu systemctl restart nginx #redhat/centos
Conclusion
Through this tutorial, we have learned how to increase or set request timeout in Nginx.