How To Fix 504 Gateway Time-out on Nginx Web Server

In this tutorial we are going to learn how to fix 504 gateway time-out on Nginx web server on Linux server. If you run a Nginx web server you may have already encountered the annoying 504 Gateway Time-out errors. This is pretty common error, are generated most probably by the PHP max execution time limit or by the FastCGI read timeout settings.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation to fix nginx 504 gateway timeout on the nginx webserver.

Fix 504 Gateway Time-out on Nginx

Changes in php.ini

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

 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):

 request_terminate_timeout = 150

Changes in Nginx Config

Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

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

 
service php-fpm restart
service nginx restart

For Nginx as Proxy for Apache web server, this is what you have to try to fix the 504 Gateway Timeout error:

Add following variables to nginx.conf file:

proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;

Once complete, simply reload Nginx:

 service nginx restart

Congratulation’s! You have successfully fix error nginx 504 gateway time out. Thanks for using this tutorial for fix 504 gateway timeout error in Linux system. For additional help or useful information, we recommend you to check the official Nginx web site.

Leave a Reply