Home > Backend Development > PHP Problem > How to solve the problem of php method execution taking too long

How to solve the problem of php method execution taking too long

藏色散人
Release: 2023-03-11 19:16:01
Original
5038 people have browsed it

Solution to the problem that the php method execution time is too long: 1. Set the Nginx gateway request timeout; 2. Set the upper limit of the PHP script execution time.

How to solve the problem of php method execution taking too long

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to solve the problem of too long execution time of the php method?

Solution to 502 Bad Gateway caused by too long execution time of PHP method

Background

Recently encountered a problem, there is a piece of PHP code that needs to involve a long IO operation of time, and this period will block the thread where the request is located, causing the request to time out.

You may say, what's so difficult about this? Just open a thread to do it asynchronously, and then it's OK to update the status. This is really a good solution, but it's a pity that this entire set of code belongs to someone else. If you want to modify the code, the business will be affected, so my solution is to solve this problem by configuring the timeout without modifying the code as much as possible.

Solution

From the above problem, the timeout is mainly affected by two factors:

  • Nginx gateway request Timeout setting

  • PHP script execution time upper limit setting

We set it in sequence.

Nginx gateway request timeout setting

Nginx supports very fine-grained timeout settings. The main ones used are as follows:

keepalive_timeout
Copy after login

Nginx uses keepalive_timeout to specify the timeout of KeepAlive. Specifies the maximum length of time each TCP connection can be maintained. The default value of Nginx is 75 seconds. Some browsers only maintain a maximum of 60 seconds, so it can be set to 60 seconds.

fastcgi_connect_timeout
Copy after login

Timeout for establishing a connection with the FastCGI server.

fastcgi_send_timeout
Copy after login

Set the timeout for transmitting requests to the FastCGI server. Set the timeout only between two consecutive write operations, not for the entire requested transfer. If the FastCGI server does not receive anything within this time, the connection is closed.

fastcgi_read_timeout
Copy after login

Timeout for reading the response from the FastCGI server. Set the timeout only between two consecutive read operations, not for the transfer of the entire response. If the FastCGI server does not transmit anything within this time, the connection is closed.

Considering that the execution of a PHP script is more relevant to the last three settings. So just add settings in the Nginx configuration file of the corresponding website.

fastcgi_connect_timeout 600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
Copy after login

PHP script execution time upper limit setting

The execution time of PHP script is mainly affected by two configurations:

php.ini 中 max_execution_time 和 max_input_time
Copy after login

php.ini The default location is /usr/local/php/etc/

Modify these two configuration items in the php.ini configuration file to the upper limit of the timeout period.

max_execution_time = 600
max_input_time = 600
php-fpm.conf 中 request_terminate_timeout
php-fpm.conf 的默认位置在 /usr/local/php/etc/
request_terminate_timeout 也设置为超时时间的上限。
request_terminate_timeout = 600
Copy after login

After the modification is completed, restart Nginx and php-fpm.

service nginx reload
/etc/init.d/php-fpm reload
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem of php method execution taking too long. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template