How to solve nginx 504 gateway timeout

Nginx suddenly reports a 504 gateway timeout error. When using PHP to perform a large number of database queries, it occasionally reports a 504 gateway timeout error. Don't be afraid of encountering a 504 gateway timeout. Only individual operations will cause a 504 gateway timeout. Refresh the timeout page, or continue to connect to the database, but it's more troublesome. Let's take a look at how to solve the Nginx 504 gateway timeout problem.

504 Gateway Timeout

Sudden error, using php toWhen performing a large number of query operations, 504 gateway timeout errors may occasionally occur. Don't be afraid of encountering 504 gateway timeouts, as only individual operations can cause 504 gateway timeouts. Refresh the timeout page, or continue to connect to the database, but it's more troublesome. Let's take a look at how to solve the Nginx 504 gateway timeout problem.

Usually the following situations will cause 504 gateway timeout:

  1. The website program processes a large amount of data, resulting in a long waiting time, causing a 504 gateway timeout.
  2. When an external request is called in a web program, a 504 gateway timeout is caused due to a timeout in the external request response.
  3. After failing to connect to the SQL database, the program was not stopped in time, resulting in an infinite loop and a 504 gateway timeout.

The 504 gateway timeout problem is caused by Nginx configuration., open the nginx configuration file. You can useorTools to log in to the server, the former is inThe latter is to modify the file through the SFTP method of SSH.

Modify nginx configuration

Modify /etc/nginx/ document.

Add the parameters in the http {} block:


fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;

Let's take a look at what these parameters do.

fastcgi_connect_timeout

Fastcgi connection timeout, default is 60 seconds

fastcgi_send_timeout

The timeout for the nginx process to send a request to the fastcgi process. The default value is 60 seconds.

fastcgi_read_timeout

The timeout for the fastcgi process to send output to the nginx process. The default value is 60 seconds.

In addition, in nginx, there areTimeout parameter settings.

proxy_connect_timeout 20000;
proxy_send_timeout 20000;
proxy_read_timeout 20000;

proxy_connect_timeout

Nginx connection timeout with backend server (proxy connection timeout)

proxy_send_timeout

Backend server data transmission time (agent sending timeout)

proxy_read_timeout

After the connection is successful, the backend server response time (proxy receiving timeout)

About Proxy TimeoutIn this part, if your Nginx does not provide external proxy services, you can ignore the configuration here.

After Nginx is modified, use nginx -t to check whether the following configuration files are correct, and then restart the nginx service.

systemctl restart nginx

Modify PHP configuration

1. Modify the /etc/php.ini file and search for max_execution_time in the php.ini file in seconds.

max_execution_time
max_execution_time is the maximum execution time of a PHP script. The default value is 30 seconds. It is recommended to change the value to 300 seconds.

2. In /etc/php-fpm.d In, modify http://www.conf File. Search for request_terminate_timeout in seconds.

request_terminate_timeout
Set the timeout for a single request. It is recommended to set it to 6000

3. You can add it to the PHP program set_time_limit(seconds) Set the maximum execution time

For example: set_time_limit(0) means no timeout.

After modifying the parameters in PHP, restart the PHP service.

systemctl restart php-fpm

Summarize

The above is the method to modify nginx 504 gateway timeout. Generally, it will cause. Through the above solution, by extending the timeout period, the 504 gateway timeout is temporarily solved. Finally, the website program and database must be optimized to be the final solution.

 

score

Leave a Reply

Your email address will not be published. Required fields are marked *