Home>Article>Operation and Maintenance> How to solve nginx 502 bad gateway problem

How to solve nginx 502 bad gateway problem

步履不停
步履不停 Original
2019-06-21 10:30:59 13091browse

How to solve nginx 502 bad gateway problem

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but due to some reason (usually a problem of reading resources) the execution has not been completed, resulting in the PHP-CGI process Termination, generally speaking, Nginx 502 Bad Gateway is related to the settings of php-fpm.conf.

Common reasons may be that the number of php-cgi processes is not enough, php execution time is long (mysql is slow), or the php-cgi process dies, and a 502 error will occur.

1. In the installed environment, a 502 problem occurs after running for a period of time. Generally, it is because the default php-cgi process is 5. It may be caused by not enough phpcgi processes. You need to modify /usr/local /php/etc/php-fpm.conf Increase the max_children value appropriately.

2. PHP execution timeout, modify /usr/local/php/etc/php.ini to change max_execution_time to 300

3. Insufficient disk space, you can use the # df -h command to check Disk usage

4. The php-cgi process died.

The usual troubleshooting methods are as follows:

1. Check the number of processes of php fastcgi (max_children value)

# netstat -anop | grep php-cgi | wc -l # netstat -anpo | grep php-fpm | wc -l

If the display is 5

2. View the current process

# ps aux | grep php-fpm 观察fastcgi/php-fpm进程数,假如使用的进程数等于或高于5个,说明需要增加。

3. Adjust the relevant settings of /usr/local/php/etc/php-fpm.conf

pm.max_children = 5 request_terminate_timeout = 60

max_children up to 5 processes , based on 20MB of memory per process, up to 100MB. That is 1 minute. If max_children increases, the more php-cgi processes will be processed quickly, and there will be fewer queued requests.

But setting max_children also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php-cgi on a server is about 20M under normal circumstances. The actual decision depends on the memory purchased for your server.

The execution time of request_terminate_timeout is 60 seconds. The request_terminate_timeout value can be set according to the performance of the server. Generally speaking, the better the performance, the higher you can set it, anywhere from 20 minutes to 30 minutes.

4. The execution time of some PHP programs exceeds the waiting time of Nginx. You can appropriately increase the FastCGI timeout time in the nginx.conf configuration file, for example:

http { ...... fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; ......}

More Nginx For related technical articles, please visit theNginx Tutorialcolumn to learn!

The above is the detailed content of How to solve nginx 502 bad gateway problem. 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