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

Symptoms

Multiple ‘504 errors’ are shown on SSL pages or it takes a lot of time to load the website. It can be fixed by restarting the ‘apache’ service, but the problem reoccurs.

In domain logs /var/www/vhosts/my.domain.com/logs/ can be found this error:

2013/11/15 03:32:13 [error] 12699#0: *5042589 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 123.123.123.123, server: my.domain.com, request: "GET /wp-login.php HTTP/1.0", upstream: "https://123.123.123.123:7081/wp-login.php", host: "my.domain.com", referrer: "https://my.domain.com/register/" 

Cause

Timeout error often is caused by issues on the backend connection that is serving the content. To fix it, you will have to figure out what configuration are you using.

Resolution

  • 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;
    

     

  • For Nginx + FastCGI (php-fpm enabled)

    Increase max_execution_time setting:

    PHP Settings – Set max_execution_time = 300

    Change request_terminate_timeout parameter (commented by default) in /etc/php-fpm.d/www.conf file:

    request_terminate_timeout = 300
    

    Add fastcgi_read_timeout variable inside ‘nginx’ virtual host configuration:

    Additional Nginx directives

    fastcgi_read_timeout 300;
    

    Add/increase the following values in the ‘http’ section of /etc/nginx/nginx.conf file:

    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 256k;
    

    Restart both ‘php-fpm’ and ‘nginx’.

发表评论