[SOLUTIONS] 504 Gateway Timeout Nginx

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:

  • “504 Gateway Timeout”
  • “504 Gateway Time-Out”
  • “504 Gateway Timeout NGINX”
  • “Nginx 504 Gateway Timeout”
  • “HTTP 504 Gateway Timeout”
  • “HTTP 504 Error”
  • “HTTP 504”
  • “Gateway Timeout (504)”

504 gateway error nginx

There are several ways to fix it:

  • For Nginx as Proxy (php-fpm disabled)To apply settings globally, increase the following timeout values by adding the file/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

  • For Nginx + FastCGI (php-fpm enabled)Increase max_execution_time setting:Plesk > Subscriptions > test.com > Websites & Domains > test.com > PHP Settings – Setmax_execution_time = 300Change 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..

  1. Open your nginx.conf file located in /etc/nginx directory.
  2. Add this below piece of code under 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.

  3. Reload Nginx and php5-fpm.
    $ 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):

Changes in PHP-FPM

Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):

Changes in Nginx Config

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

Reload PHP-FPM and Nginx


Read also:


[SOLUTIONS] 502 Bad Gateway Error on Nginx


nginx 504 error: Connection timed out

Prevent nginx 504 Gateway timeout using PHP set_time_limit()

How To Fix 504 Gateway Time-out on Nginx

Related Posts

Recommended File Permissions for WordPress

July 31, 2017

Security, Tutorial, WordPress

What permissions should I have for the following: Root folder storing all the WordPress content wp-admin wp-content wp-includes On computer filesystems, different files and directories have permissions that specify who and what can read, write, modify and access them. This is important because WordPress may need access to write to files in your wp-content directory to enable certain functions. […]

Read More

How to Prevent SQL injection in PHP 2017

July 31, 2017

PHP, Security, Tutorial

SQL injection happens when you interpolate some content into a SQL query string, and the result modifies the syntax of your query in ways you didn’t intend. It doesn’t have to be malicious, it can be an accident. But accidental SQL injection is more likely to result in an error than in a vulnerability. The […]

Read More