Nginx + php-fpm – Getting Internal server error 504

Published on Author gryzli

Nginx + php-fpm (and not only) could produce Internal Server Error 504.

If you happen to get this error, usually after waiting about 60 seconds(this is the default timeout for lots of timeout directives) for some php operation, then you can try to rise the following limits:

1. Rising php.ini limits

Make sure to increase the following directives in your php.ini:

max_execution_time = 1000;  # (1000 seconds, set it to a value best for your needs)

max_input_time = 1000; # (1000 seconds, set it to a value best for your needs)


You may also rise memory_limit, cause most of the time long running operations take their piece of memory too.

memory_limit = 256M; # (256M , set it to a value best for your needs)


After increasing your php.ini limits, make sure to restart php-fpm process. On most RHEL/Fedora/Centos linux distros, this could be done by with command such as: 

# /etc/init.d/php-fpm restart 

2. Rising PHP-FPM timeout

Open your php-fpm.conf for editing and make sure you have this directive:

request_terminate_timeout = 1000;  # (1000 seconds, set it to a value best for your needs)

Then make sure to restart PHP-fpm.


3. Rising Nginx Fastcgi read/send timeout

The next step is rising Nginx fastcgi limits. This must be done in your location{} section, which process your PHP files and sends them to the PHP-FPM server (socket or TCP).

The directives are:

fastcgi_read_timeout 1000; 

fastcgi_send_timeout 1000; 

Here is some example of a location processing PHP requests in Nginx:

location ~ \.php(/.*)?$ {
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass “unix:/tmp/php-fpm.sock”;
include /etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 1000;

fastcgi_send_timeout 1000;

Restart nginx:

#/etc/init.d/nginx restart


Whatever changes you are doing, don’t forget to restart your PHP-FPM and Nginx:

# /etc/init.d/nginx restart

# /etc/init.d/php-fpm restart