How to fix error: 504 Gateway Time-out on Nginx


Sometimes, you get the error: 504 Gateway Time-out on Nginx like this figure, how to fix it.

504 gateway time out

This is simple. You follow 2 below steps:
Step 1: Add this line to your PHP file

set_time_limit(0);

or

set_time_limit(120);

You can change 120 to the seconds you want.

Step 2:
You edit the file .conf like /etc/nginx/conf.d/default.conf, you will see the content like:

server {
    listen       80;
    server_name  localhost;
    root   /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php;
    }
    

    location ~ \.php$ {
        try_files $uri =404;
		fastcgi_pass unix:/tmp/php_fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
		
		fastcgi_connect_timeout 60;
        fastcgi_send_timeout 60;
        fastcgi_read_timeout 60;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        #fastcgi_buffers 256 4k;        
        #fastcgi_max_temp_file_size 0;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;
		
    }
	
}

You inscrease the value of fastcgi_read_timeout to the value like 600.

Now, you restart Nginx and everything works well.

Leave a Reply