504 Gateway Timeout error Nginx is generated often by a number of reasons on the backend connection that is serving content. This is pretty common error, are generated most probably by the PHP max execution time limit or by the FastCGI read timeout settings. Based on Wikipedia, 504 Gateway Timeout is the server was acting as a gateway or proxy and did not receive a timely response from the upstream server.. In previous post, I’ve write How to Fix 502 Bad Gateway Error on Nginx.
Here are the most common 504 error messages:
There are several ways to fix it:
/etc/nginx/conf.d/timeout.conf
and restarting ‘nginx’ service:# cat /etc/nginx/conf.d/timeout.conf
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
If you only are able to increase timeout settings per domain, it can be done in this way:
Plesk > Subscriptions > my.domain.com > Websites & Domains > Web Server Settings – add the lines to Additional Nginx directives
max_execution_time
setting:Plesk > Subscriptions > test.com > Websites & Domains > test.com > PHP Settings – Setmax_execution_time = 300
Change request_terminate_timeout
parameter (commented by default) in /etc/php-fpm.d/www.conf
(for Debian /etc/php5/fpm/pool.d/www.conf
) file:request_terminate_timeout = 300
Add fastcgi_read_timeout
variable inside the ‘nginx’ virtual host configuration:
Plesk > Subscriptions > my.domain.com > Websites & Domains > Web Server Settings > Additional Nginx directives
fastcgi_read_timeout 300;
Add/increase the following values in the ‘http’ section of the /etc/nginx/nginx.conf
file:
fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;
Restart both ‘apache’ and ‘nginx’.
==========================================
If above steps doesn’t work, try this one..
nginx.conf
file located in /etc/nginx
directory.http {
section:client_header_timeout 3000;
client_body_timeout 3000;
fastcgi_read_timeout 3000;
client_max_body_size 32m;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 128k;
Note: If its already present , change the values according.
$ service nginx reload
$ service php5-fpm reload
If the error persists, consider increasing the values.
=========================================
Changes in php.ini
Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):
1 | max_execution_time = 150 |
Changes in PHP-FPM
Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):
1 | request_terminate_timeout = 150 |
Changes in Nginx Config
Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:
1 2 3 4 5 6 7 | location ~* \.php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_read_timeout 150; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } |
Reload PHP-FPM and Nginx
1 2 | service php–fpm restart service nginx restart |
Read also:
Source:
nginx 504 error: Connection timed out
Prevent nginx 504 Gateway timeout using PHP set_time_limit()
admin
April 25, 2016
Nginx, PHP, Tutorial
17 Comments