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...

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...
- Fix 504 Gateway Timeout Nginx Error
- Other Error Fixes
Interactive Builder for this Guide
Build your custom nginx syntax (Fix 504 Gateway Timeout Nginx Error - 5 Simple Steps)
server {
listen 80;
listen [::]:80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
gzip on;
gzip_types text/plain text/css application/json application/javascript application/xml;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}server_nameBinds this virtual host to app.example.comreturn 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPSproxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application serverX-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjackingIf 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
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.)
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
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
- Error Establishing a Database Connection in WordPress Fix
- 4 Simple Ways to Fix 502 Bad Gateway Nginx Error in Ubuntu
- PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)
- Your PHP installation appears to be missing the MySQL extension which is required by WordPress
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].

UNIX and Linux System Administration Handbook, 5th Edition
Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
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.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Zip a Folder in Ubuntu Linux / Debian Linux
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides











Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!