Believe in yourself, come on!
****
nginx + php appeared 502 bad gateway, nginx general this is not a problem, but because of problems caused by fastcgi or php
The reason is that the server connection timeout occurs 502 we send a request to the server because the server is currently too many links, resulting in the server side can not give a normal response, generate such an error
So if you are a very large amount of concurrent server, it can only increase the machine first, then refined by way will achieve better results; but if you did not appear concurrent 502, can generally be attributed to configuration issues, script timeout issue.
****
1、memory_limit /etc/php/7.2/cli/php.ini
If there are individual processes php program need to take great care must be taken in this memory
2、max_children php-fpm.conf
Unreasonable, php-fpm number of processes is not enough, set too small because there is not enough cgi process to handle the request, set off
Assembly while there are cases appear normal response, and so on for a long time while only response, use netstat -napo | grep "php-
fpm "| wc -l look at the current number of fastcgi process, if the number close to the upper limit in conf configuration, you need to increase
The number of processes (children in accordance with the in-memory computing, such as 1G set 64,2G128 under normal circumstances. This according to the actual situation
Self-adjusting. )
3、pstream sent too big header while reading response headerfrom upstream nginx日志
Check the client head buffer, fastcgi buffer size is too small
4, timeout execution time is too long timeout PHP program
Check nginx fastcgi and various timeout settings. nginx in fastcgi_connect_timeout 300;
fastcgi_send_timeout 300 fastcgi_read_timeout300 keepalive_timeout ;
php-fpm in request_terminate_timeout, php.ini in max_execution_time
5、max_requests php-fpm
This parameter specifies how many will be turned off after each request up to deal with children. In a large number of processing requests, if the value
Setting too small will lead to the establishment of children frequent suicide and wasted a lot of time, if almost all of the children in this
When a suicide, children will not respond to the request before the reconstruction, so there 502. This value can be set larger or
0 [infinity].
6, increase the number of open file linux kernel
(Must be the root account)
echo 'ulimit -HSn 65536'>> /etc/profile
echo 'ulimit -HSn 65536'>> /etc/rc.local
source /etc/profile
7, script execution timeout
If the script wait a long time for some reason it does not return, resulting in new requests can not be processed, tuning the following configurations.
nginx.conf 里面主要是如下
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
php-fpm.conf 里如要是如下
request_terminate_timeout =10s
8、缓存设置较小
修改或增加配置到 nginx.conf
proxy_buffer_size 64k;
proxy_buffers 512k;
proxy_busy_buffers_size 128k;
9、recv()failed (104: Connection reset by peer) while reading response header
fromupstream
最重要的是程序里要设置好超时,不要使用 php-fpm 的 request_terminate_timeout ,
最好设成 request_terminate_timeout=0;
因为这个参数会直接杀掉 php 进程,然后重启 php 进程,这样前端 nginx 就会返回 104:
Connection reset by peer 。这个过程是很慢,总体感觉就是网站很卡。
还应该注意到php.ini中的max_execution_time参数。当请求终止时,也会出现502错误的。
10、调整增大php 和Nginx 的backlog数
listen.backlog = 4096