Increasing web server timeout

Posted on 25-09-2018 by Nadir Latif


Source: https://stackoverflow.com/questions/24453388/nginx-reverse-proxy-causing-504-gateway-timeout

If your website is based on Php and is being served by Apache web server with Nginx as reverse proxy, then you may have experienced 504 Gateway Timeout errors. The error usually happens because some Php script takes longer than usual to run. If you want to allow the Php script to run for more than the default time of 30 seconds, then you have to make some changes to the Php.ini configuration and the nginx.conf and nginx virtual host configuration.

To increase timeout of Php scripts, we need to increase the max_execution_time settings in php.ini. The default value is 30 seconds. The changes will take affect after the Apache web server has been restarted

The timeout also has to be configured on the Nginx reverse proxy. It requires disabling the keepalive probes sent by Nginx to Apache. This can be done by adding the "Connection: '' http header with empty value. For example:

proxy_set_header Connection "";.

This needs to be set under the location block of the virtual host file located under /etc/nginx/sites-available

The fastcgi read timeout and proxy read timeout settings also need to be increased in the Nginx configuration. If you are trying to upload large files, then you need to increase the client_max_body_size setting. This can be done by adding the following:


fastcgi_read_timeout 600;
proxy_read_timeout 600;
client_max_body_size 20M;
to /etc/nginx/nginx.conf. After making the changes, nginx needs to be restarted.