504 Gateway Time-out using Nginx

It is very common to see a 504 Gateway Time-out using Nginx webserver. This timeout error is generated often by a number of reasons on the backend connection that is serving content. To fix it, you will have to figure out what configuration are you using.

For Nginx + FastCGI (php-fpm), you should try to tweak nginx configuration in this way-:

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

max_execution_time = 300

But, you should also change set request_terminate_timeout parameter (commented by default) at www.conf file from PHP-FPM:

pico -w /etc/php-fpm.d/www.conf

Then set the variable to the same value as max_execution_time:

request_terminate_timeout = 300

Now let’s add  fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~ .php$ {
root /var/www/sites/nginxtips.com;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}

Then restart nginx:

service nginx reload

For Nginx as Proxy for Apache web server, this is what you have to try:

Add this variables to nginx.conf file:

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

Then restart nginx:

service nginx reload

Popular search terms:

  • 504 gateway time-out (1)
  • 504 gateway time-out nginx (1)
  • 504 gateway timeout nginx (1)


16 Comments

  1. Hello,

    I have linux server and I am getting this “504 Gateway Time-out” error on my website. Actually I am Windows user and I am not understanding linux. I can’t fix it this error. Could you write more detail please how can I do that?

    Thanks

  2. Do you have to set
    proxy_connect_timeout 600;

    from nginx docs
    http://wiki.nginx.org/HttpProxyModule#proxy_connect_timeout

    “This directive assigns a timeout for the connection to the upstream server. It is necessary to keep in mind that this time out cannot be more than 75 seconds. ”

    So the upper bound is 75 no?

  3. Thanks for this. May I make the suggestion though of covering where the Nginx virtual host configuration file is normally? I know everybody’s install is usually different but gotta admit that it took me a few minutes to remember where mine was. For me, it was in the /etc/nginx/sites-available directory.

    • Mike

      You mentioned ‘normally’, but what is normal to you using /etc/nginx/sites-available (usually on Debian/Ubuntu) is not normal for RHEL based users like Fedora, CentOS and RHEL itself. And it’s also different when you build nginx from the source gz files. It’s impossible to cover all posible nginx directory installations.

      Thanks for your comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>