深入探讨:Nginx 502 Bad Gateway错误的解决方法_PHP
Nginx
max_children=40 , 每个children平均占用20M-30M内存,children越多,可以同时接受的并发数量越多,一般children的值是网站最高并发数+浮动值,这值再×内存占用,就是你需要用到的内存。max_requests = N 是指当每个children接受了N次请求以后,就会把自己杀死,然后重新建立一个children。
PV / max_children = 每一个children接受的request次数[ 默认预设浏览一个只调用一次PHP程序,或许异步调用呢?接口呢?]
比如上面的值是1000,而你定义的是10240,那么fpm要超过10天才能杀死children并重建,这样如果存在内存泄露的话,就会导致进程占用过多的内存而无法释放,从而使fpm的处理能力降低,还会产生一些莫名其妙的错误。
但是如果你把这个值设置的过小,fpm频繁的杀死children并重建,也会导致额外的开销。
最好的优化当然是根据你网站的运行情况,去不断的调试,找到一个平衡点。
针对max_children还有一个偷懒的做法,如果你的php是5.3,那么你可以把fpm的style设置为apache-like,这个时候children的数量就由fpm自动控制。相应的配置参数是
start_servers:起始进程数量
min_spare_servers:最小进程数量
max_spare_servers:最大进程数量
当服务器比较空闲的时候,fpm会主动杀死一些多余的children,用来节约资源,当服务器繁忙的时候,服务器会自动建立更多的children。
#########################
Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止,
一般来说Nginx 502 Bad Gateway和php-fpm.conf的设置有关。
php-fpm.conf有两个至关重要的参数,一个是max_children,
另一个是request_terminate_timeout,但是这个值不是通用的,而是需要自己计算的。
在安装好使用过程中出现502问题,一般是因为默认php-cgi进程是5个,可能因为phpcgi进程不够用而造成502,需要修改/usr/local/php/etc/php-fpm.conf 将其中的max_children值适当增加。
计算的方式如下:
如果你的服务器性能足够好,且宽带资源足够充足,PHP脚本没有死循环或BUG的话你可以直接将 request_terminate_timeout设置成0s。0s的含义是让PHP-CGI一直执行下去而没有时间限制。而如果你做不到这一点,也就 是说你的PHP-CGI可能出现某个BUG,或者你的宽带不够充足或者其他的原因导致你的PHP-CGI假死那么就建议你给request_terminate_timeout赋一个值,这个值可以根据服务器的性能进行设定。一般来说性能越好你可以设置越高,20分钟-30分 钟都可以。
而max_children这个值又是怎么计算出来的呢?这个值原则上是越大越好,php-cgi的进程多了就会处理的很快,排队的请求就会很少。 设置max_children也需要根据服务器的性能进行设定,
一般来说一台服务器正常情况下每一个php-cgi所耗费的内存在20M左右。
按照官方的答案,排查了相关的可能,并结合了网友的答案,得出了下面的解决办法。
1、查看php fastcgi的进程数(max_children值)
代码:netstat -anpo | grep “php-cgi” | wc -l
5(假如显示5)
2、查看当前进程
代码:top
观察fastcgi进程数,假如使用的进程数等于或高于5个,说明需要增加(根据你机器实际状况而定)
3、调整/usr/local/php/etc/php-fpm.conf 的相关设置
max_children最多10个进程,按照每个进程20MB内存,最多200MB。
request_terminate_timeout执行的时间为60秒,也就是1分钟。
#################################################
网站运行环境是Nginx +php fastcgi模式的。这几天运行一直不稳定,总是出错,报502错误。
今天跟以前的同事请教了一下,他告诉我检查一下php-fpm的日志,那里记录了很多有用的信息。
于是我检查了一下,发现确实有很多报错信息:
Sep 30 08:32:23.289973 [NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200, cur:51200
如果和nginx.conf : worker_rlimit_nofile 65500; 不一致必须检查,设置重启服务
Mar 01 14:39:15.881047 [NOTICE] fpm_children_make(), line 352: child 12364 (pool default) started
Mar 01 14:39:21.715825 [NOTICE] fpm_got_signal(), line 48: received SIGCHLD
Mar 01 14:39:21.715899 [NOTICE] fpm_children_bury(), line 215: child 11947 (pool default) exited with code 0 after 175.443305 seconds from start
有的报错信息,就好说了,直接上网查信息。
经过搜索,最后总结出以下几条优化策略:
1、提升服务器的文件句柄打开打开
# vi /etc/security/limits.conf 加上
* soft nofile 65500
* hard nofile 65500
2、提升nginx的进程文件打开数
nginx.conf : worker_rlimit_nofile 65500;
3、修改php-fpm.conf文件,主要需要修改2处。
命令 ulimit -n 查看限制的打开文件数,php-fpm.conf 中的选项rlimit_files 确保和此数值一致。
4、
# vi /etc/sysctl.conf
底部添加
fs.file-max=65500
经过以上修改,重启PHP。/usr/local/webserver/php/sbin/php-fpm restart
在查看ulimit -n 是否生效,否则重启服务器或者/etc/sysctl.conf、/etc/security/limits.conf的配置生效
到目前为止还没有出现过以上的报错信息。一切运行正常。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
