How to Fix 504 Gateway Timeout in Nginx Server

I use NGINX a lot. I recently deployed a Node.js web application with NGINX as a reverse proxy server for it. One of the key features of the application is support for data imports using excel templates. However, it didn’t take long before users uploading bulky files started getting a 504 Gateway Timeout error from NGINX.

[ You might also like: Fix Nginx Error: 413 Request Entity Too Large ]

Are you getting the same error? Don’t worry, I have got you covered. In this article, I will show how to fix the 504 Gateway Timeout error by increasing the request timeout in the NGINX web server.

Increase Request Timeout in NGINX for a Proxied Server

If you are using NGINX as a reverse proxy for an application server such as Node.js or a web server such as Apache or Gunicorn, then you can increase request timeout by setting the following parameters either in the http, or server, or location directive.

The timeout is in seconds and makes sure that you have to set timeout values that will work effectively and efficiently for your environment.

proxy_connect_timeout 75;
proxy_send_timeout 600;
proxy_read_timeout 600;

From the names of the directives, it is easy to tell what timeout they define. The proxy_connect_timeout directive states a timeout for creating a connection with a proxied server. According to the official NGINX documentation, the value should not exceed 75 seconds.

The next directive proxy_send_timeout defines a timeout for transmitting a request to the proxied server. The last directive proxy_read_timeout sets a timeout for reading a response from the proxied server.

Now that you have a slight understanding of the above directives, you can configure that as shown. In the http context, you can set them in NGINX’s main configuration file located at /etc/nginx/nginx.conf.

http{
....
	proxy_read_timeout 600;
	proxy_connect_timeout 600;
	proxy_send_timeout 600;
....
In the server context, you can define them in a server block file for your application for example /etc/nginx/conf.d/example.com.conf:
server{
....
             proxy_read_timeout 600;
	 proxy_connect_timeout 600;
	 proxy_send_timeout 600;

             location  /  {
                try_files $uri $uri/ /index.html =404 =403 =500;
          }
          location  /api  {
                proxy_pass http://127.0.0.1:5000;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                
       }
}

In a location block, they would look like this:

server{
....
             location  /  {
                try_files $uri $uri/ /index.html =404 =403 =500;
          }
          location  /api  {
                proxy_pass http://127.0.0.1:5000;
                proxy_read_timeout 600;
	        proxy_connect_timeout 600;
	        proxy_send_timeout 600;

               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header Host $host;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
}

Save the file and close it.

To apply the recent changes, you need to restart the NGINX service.

$ sudo systemctl restart nginx
OR
# systemctl restart nginx

Increase Request Timeout in NGINX for FastCGI

For a FastCGI server such as PHP-FPM, you can use the following directive either in the http, or server, or location:

fastcgi_connect_timeout 75;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;

In a location block for processing PHP files, you can define them as shown:

location ~ .php$ {
	fastcgi_pass unix:/run/lib/php7.4-fpm.sock;
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	include fastcgi_params;
	fastcgi_connect_timeout 75;
            fastcgi_send_timeout 600;
            fastcgi_read_timeout 600;
}

Do not forget to restart the NGINX service after making changes:

$ sudo systemctl restart nginx
OR
# systemctl restart nginx

Note: You might also have to make some configuration changes in the php.ini and PHP-FPM pool configuration files especially to the max_execution_time and request_terminate_timeout directives respectively.

That’s it! In this article, we looked at how to increase request timeout in NGINX to solve the 504 Gateway timeout error. For more information, read the NGINX documentation. For any comments, reach us via the feedback form below.

Got something to say? Join the discussion.