[SOLUTIONS] 504 Gateway Timeout Nginx


April 25, 2016

Nginx, PHP, Tutorial

No Comment

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

Finding a DropShipping Wholesaler

June 5, 2016


So now you are probably wondering how you can find and qualify a genuine wholesale drop shipper. You will need some patience and time to search through a drop shipper directory in order to find the best drop shipping companies for your particular product niche. There are many drop shipper directories available on the Internet, […]

Read More

Enabling/Installing FFmpeg Extension PHP From cPanel

May 29, 2016

PHP, Tutorial

FFmpeg is php extension used to manipulate videos files in PHP using the ffmpeg program. It provides classes for retrieving information from video and audio files like the duration, frame and sample rate, frame size. It can also extract individual frames from the videos and create GD image resources. It supports the all the video formats supported by ffmpeg […]

Read More