Gateway Timeout

How to fix the Gateway Timeout error on Nginx web server.

The Gateway Timeout error message may appear under different 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)

Fix Gateway Timeout

In order to fix the Gateway Timeout, there are a couple of solutions you can follow.

If you use PHP-FPM

Increase max_execution_time, add fastcgi_read_timeout

Edit the PHP php.ini file to edit the max_execution_time parameter:

vi /etc/php.ini

Find the max_execution_time parameter and increase the value:

max_execution_time = 300

Edit the server {} block directive to use fastcgi_read_timeout parameter:

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

Reload the configuration file to test it:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

If Nginx is used as a proxy server

If Nginx web server is used as a proxy server, update the nginx.conf configuration file to increase the timeouts:

server {
    location {
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;
    }
}

Reload the configuration file to test it:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx