Fix 504 Gateway Timeout Nginx Error

969 0
504 Gateway Timeout Nginx Error
Share is caring:

You might see 504 Gateway Timeout Nginx Error If you have a large website setup or a server with limited resources. It means that a server upstream that is acting as a gateway to handle HTTP requests is either down or too slow to manage requests.

This issue is really frustrating and sometimes you do not find any clue to how to resolve it. You even might need a help of webmaster or ISP.

I faced the similar issue with my WordPress website, and it took me two days to resolve. Here are the steps that you can follow to increase the time and handle HTTP requests.

Steps Required to Fix 504 Gateway Timeout Nginx Error

1. Changes in php.ini

You need to make a change in the php.ini files. PHP default is the 30s and you need to increase it to 300.

Open the php.ini file using Vim editor by using the following command:

For Ubuntu 14.04

sudo vim /etc/php5/fpm/php.ini

For Ubuntu 16.04 LTE

sudo vim /etc/php/7.0/fpm/php.ini

Now, search for the max_execution_time (press / and type max_execution_time).

WP Engine hosting

Find the right line and change the max_execution_time to 300. (press “i”, navigate to the line, make the change and save it by pressing ESC and entering “wq!” then enter.)

max_execution_time = 300

If you are using Apache, applications running PHP as a module above would be adequate. But in case you are using NGINX, we need to make this change at 2 more places. So let’s find out where to make these changes.

2. Changes in PHP-FPM

This step is only required if you have already un-commented request_terminate_timeout parameter. By default, it is commented out and takes the value of max_execution_time found in php.ini.

Now make changes inside the www.conf file by running the following command:

For Ubuntu 14.04

sudo vim /etc/php5/fpm/pool.d/www.conf

For Ubuntu 14.04

sudo vim /etc/php/7.0/pool.d/www.conf

Now, search for the request_terminate_timeout and set request_terminate_timeout to 300.

request_terminate_timeout = 300

3. Changes in Nginx Config

Now we need to increase the time limit for a website that you are hosting on this server inside the sites-available directory. e.g.,

sudo vim /etc/nginx/sites-available/


location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;


location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

         # With php5-fpm:
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_read_timeout 300;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

In case your are you are running multiple websites on a single server, you do not need to manually edit the every file to increase the time-limit instead you can edit the main nginx.conf file inside Nginx directory:

sudo vim /etc/nginx/nginx.conf

Add following in http{..} section

http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

5. Reload PHP-FPM & Nginx

Now, reload the PHP-FPM and Nginx by running the following commands:

Using SysV Init scripts directly:

/etc/init.d/php-fpm restart    # typical
/etc/init.d/php5-fpm restart   # debian-style
/etc/init.d/php7.0-fpm restart # debian-style PHP 7

Using service wrapper script

service php-fpm restart    # typical
service php5-fpm restart   # debian-style
service php7.0-fpm restart # debian-style PHP 7

Using Upstart (e.g. ubuntu):

restart php7.0-fpm         # typical (ubuntu is debian-based) PHP 7
restart php5-fpm           # typical (ubuntu is debian-based)
restart php-fpm            # uncommon

Using systemd (newer servers):

systemctl restart php-fpm.service    # typical
systemctl restart php5-fpm.service   # uncommon
systemctl restart php7.0-fpm.service # uncommon PHP 7

This should solve the NGINX PHP5-fpm 504 Gateway Timeout Error. If the error still persists after some time go back to the nginx.conf file inside Nginx directory by running the following command:

sudo vim /etc/nginx/nginx.conf

Now add some more parameters inside http{..} section

http {
Client_header_timeout 300;
Client_body_timeout 300;
Client_max_body_size 32m;

This should resolve 504 Gateway Timeout Nginx Error. If you still find the same issue, I would suggest you contact your ISP provider or find a webmaster who can resolve this issue. There might be some other configuration issues.

If you have any other way to fix this issue, please share them with our audience in the comment section below or contact me on my email [email protected].

Thanks for reading!

Share is caring:
In this article

Join the Conversation