How to Increase Request Time Out in NGINX


increase request timeout nginx

By default, NGINX times out after 60 seconds. So if some of the requests to your website take a long time to run, then your visitors will most likely see a “504 Gateway Timeout” error message. Luckily, it is quite easy to fix this problem. Let us take a look at how to increase request Time Out in NGINX.

 

How to Increase Request Time Out in NGINX

By default, NGINX waits for 60 seconds to get a response from its upstream server (e.g Apache). If it doesn’t get a response during that time frame, it will close the connection and send a”504 Gateway Timeout” message. It does not include the time taken to transmit the response data.

The most common solution to this problem is to simply increase the request timeout in NGINX. Then you can take the time to investigate the script that takes too long to complete, and optimize it if possible.

 

You can increase request timeout in NGINX using the proxy_read_timeout directive. You can place it in any of the http, server, location contexts.

Here the Syntax:


proxy_read_timeout time;

 

Here’s an example to increase timeout to 300 seconds


proxy_read_timeout 300s;

 

Here are the steps to increase request timeout in NGINX

 

1. Open your NGINX config file in a text editor


$ sudo vim /etc/nginx/nginx.conf

 

2. Add the proxy_read_timeout directive to any of the http, server or location blocks

  • http – applicable to all websites hosted by NGINX
  • server – applicable to the specific server/host/website
  • location – applicable only to the specific folder or subdirectory, and its children.

 


server {
 listen 80;

 proxy_read_timeout 300s;

 root /var/www/example.com/httpdocs;
 index index.html index.htm;

 server_name example.com www.example.com;
}

 

3. Reload NGINX Server


$ sudo service nginx reload

 

That’s it!

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!