Home>Article>Backend Development> 502 error occurs when php connects to the database

502 error occurs when php connects to the database

尚
Original
2019-10-28 16:52:22 3222browse

502 error occurs when php connects to the database

nginx php 502 bad gateway appears. Generally, this is not a problem with nginx, but is caused by a problem with fastcgi or php. The common ones are as follows.

1. The memory_limit of php.ini is too small (you must pay attention to this if there are individual php program processes that need to occupy a large amount of memory)

2. Max_children or max_requests settings in php-fpm.conf Unreasonable (If the setting is too small, there will not be enough cgi processes to handle the request. If the setting is too high, the response will be normal for a while, and then it will take a long time to respond. Generally, children are calculated according to memory, for example, 1G is set to 64, 2G128. This is adjusted according to the actual situation.

In addition, the command to check whether the current number of PHP FastCGI processes is sufficient is: netstat -anpo | grep “php-cgi” | wc -l If the actual number of "FastCGI" is used The number of processes" is close to the preset "number of FastCGI processes", then it means that the "number of FastCGI processes" is not enough and needs to be increased.)

3. Check the nginx error log and find that pstream sent too big header while reading response header from upstream, check whether the client head buffer and fastcgi buffer size are too small, which can be set to 32K.

4. The php program execution time is too long and times out. Check the various timeout settings in nginx and fastcgi.

(fastcgi_connect_timeout 300 in nginx; fastcgi_send_timeout 300: fastcgi_read_timeout300; keepalive_timeout; request_terminate_timeout in php-fpm, max_execution_time in php.ini)

5. php-fpm has a parameter max_requests, This parameter specifies the maximum number of requests each child can handle before being closed. When processing a large number of requests, if this value is set too small, it will cause children to commit suicide and create frequently, which wastes a lot of time. If all children commit suicide at almost this time, there will be no children to respond to the request before reconstruction, so 502 will appear. You can set this value larger or 0 [infinite].

Recommended:php server

The above is the detailed content of 502 error occurs when php connects to the database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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