The recent 502 error of Nginx Gap, how to implement automatic restart PHP-FPM?
Think of the solution
1. Execute shell script with crontab timer, reboot after error (timed execution every 5 seconds)
2. Using the Nohup,shell script background execution
Sample Script
#!/bin/bash while : do URL="http://192.168.1.30" RESULT=`curl -m 10 -I -s $URL | grep "HTTP/1.1 502"` if [ -n "$RESULT" ]; then /etc/init.d/php-fpm restart fi
3. Write Nginx module, execute shell script by condition
Can think of is that these kinds of, feel that these kinds of programs are not very good, who has a better solution?
Reply content:
The recent 502 error of Nginx Gap, how to implement automatic restart PHP-FPM?
Think of the solution
1. Execute shell script with crontab timer, reboot after error (timed execution every 5 seconds)
2. Using the Nohup,shell script background execution
Sample Script
#!/bin/bash while : do URL="http://192.168.1.30" RESULT=`curl -m 10 -I -s $URL | grep "HTTP/1.1 502"` if [ -n "$RESULT" ]; then /etc/init.d/php-fpm restart fi
3. Write Nginx module, execute shell script by condition
Can think of is that these kinds of, feel that these kinds of programs are not very good, who has a better solution?
Inspired by the Fastcgi_next_upstream parameter, using the PHP-FPM thread pool concept, the 502 error can be solved perfectly (http_502 is not )
The configuration inside HTTP
upstream php_fpm_sock{ server unix:/dev/shm/php-fpm.socket; server unix:/dev/shm/php-fpm-b.socket; server unix:/dev/shm/php-fpm-c.socket;} fastcgi_next_upstream error timeout invalid_header http_503 http_500;
Server inside Fastcgi_pass Configuration
location ~* \.php$ { fastcgi_pass **unix:php_fpm_sock;** fastcgi_index index.php; client_max_body_size 50M; client_body_temp_path /data/www/tmp; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi.conf; include fastcgi_params;}
Configuration of the PHP-FPM
#/etc/php-fpm.conf 文件包含多个配置文件include=/etc/php-fpm.d/*.conf#/etc/php-fpm.d/ 目录www-a.confwww-b.confwww-c.conf#配置,三个文件这里不一致,分别对应#Start a new pool named www-a [www-a]listen = /dev/shm/php-fpm.socket
Ps-ef View
www 17996 31539 0 12:13 ? 00:00:51 php-fpm: pool www-bwww 17999 31539 0 12:13 ? 00:00:48 php-fpm: pool www-awww 18010 31539 0 12:14 ? 00:00:46 php-fpm: pool www-bwww 18063 31539 0 12:25 ? 00:00:42 php-fpm: pool www-cwww 18153 31539 0 12:47 ? 00:00:34 php-fpm: pool www-cwww 18154 31539 1 12:47 ? 00:00:37 php-fpm: pool www-awww 18185 31539 0 12:55 ? 00:00:29 php-fpm: pool www-cwww 18313 31539 0 13:24 ? 00:00:10 php-fpm: pool www-a
1, the start of each PHP-FPM thread pool, as long as not all hung, nginx can be normal execution PHP, if some abnormal exit, the basic does not affect the site run
2, fastcgi_next_upstream that row of parameters, do not need to add http_502, the actual you also add not up
3, the original each section similar to this location ~ \.php$ {} code needs to fastcgi_pass this line according to the example transformation
Nginx can configure Fastcgi_next_upstream to implement failover, such as:
fastcgi_next_upstream error timeout invalid_header http_500 http_503;
Back-end PHP-FPM return error, timeout and other information will automatically switch to upstream in the next PHP-FPM application server.
Personally, it is best to find out why the PHP-FPM work process crashes, whether it is a code problem or a response time-out due to insufficient system resources.
Note Two, one is not to execute long or indeterminate code in PHP-FPM, such as curl making a network request. Second, PHP-FPM work process is not the more the better, personally think, php-fpm work process number, set to twice times the CPU core number is sufficient. After all, Nginx and MySQL and the system also consume CPU. It is very unreasonable to set the number of PHP-FPM processes based on server memory, it is obviously more appropriate to allocate memory to MySQL, memcached these services, and too many php-fpm processes will increase the overhead of CPU context switching.