Website Vidya
Linux & ServersStep-by-Step Guide

Fix 504 Gateway Timeout Nginx Error - 5 Simple Steps

If you have a large website setup or a server with limited resources, 504 Gateway Timeout Nginx Error can be seen quite often. It means that a server upstream that is acting as a gateway to handle HTTP requests is either down or too slow to...

RR
Rajesh RayLead Systems Educator
Published:
4 min read
Fix 504 Gateway Timeout Nginx Error - 5 Simple Steps
AI Executive SummaryCORE CIS AI

4 min read

If you have a large website setup or a server with limited resources, 504 Gateway Timeout Nginx Error can be seen quite often. It means that a server upstream that is acting as a gateway to handle HTTP requests is either down or too slow to...

Key Highlights
  • Fix 504 Gateway Timeout Nginx Error
  • Other Error Fixes
Grounded in full engineering runbookCORE CIS Ai Engine

If you have a large website setup or a server with limited resources, 504 Gateway Timeout Nginx Error can be seen quite often. 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 the help of webmaster or ISP.

I faced a 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.

Setting up error logs

Advertisement

Note: Setting up error logs plays a significant role in identifying server errors. You can check the official documentation of NGINX to configure error logging.

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 or PHP 5

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

For Ubuntu 16.04 LTE or PHP 7.0

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

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

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 typing wq! then enter.)

Advertisement
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 or PHP 5

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

For Ubuntu 16.04 LTE or PHP 7.0

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., example.com

Advertisement
sudo vim /etc/nginx/sites-available/example.com

For PHP5-FPM

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

For PHP7-FPM

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 you are running multiple websites on a single server, you do not need to manually edit the every example.com 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 a 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 the 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.

Other Error Fixes

If you have any other way to fix 504 Gateway Timeout Nginx Error, share with us in the comment section below or contact me on my email [email protected].

RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Linux & Servers•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout