PHP的502报错

相信自己,加油!


****

 nginx+php 出现502 bad gateway,一般这都不是nginx的问题,而是由于 fastcgi或者php的问题导致的

  服务器出现 502 的原因是连接超时 我们向服务器发送请求 由于服务器当前链接太多,导致服务器方面无法给于正常的响应 , 产生此类报错

因此如果你服务器并发量非常大,那只能先增加机器,然后按以下方式优化会取得更好效果 ; 但如果你并发不大却出现 502 ,一般都可以归结为配置问题,脚本超时问题。

****

 

 

1、memory_limit       /etc/php/7.2/cli/php.ini

     如果有个别php程序进程需要占用极大内存时这个必须注意

 

2、max_children           php-fpm.conf

     设置不合理,php-fpm 进程数不够用,设置过小会因为没有足够的cgi进程处理请求,设置过

     大会出现一会儿有响应正常,一会儿等很久才有响应的情况,使用 netstat -napo |grep "php-

     fpm" | wc -l 查看一下当前 fastcgi 进程个数,如果个数接近 conf 里配置的上限,就需要调高

     进程数(一般情况下children 按照内存计算,比如说1G设置64,2G128。这个根据实际情况

     自行调整。)

 

3、pstream sent too big header while reading response  headerfrom upstream    nginx日志

    检查client head buffer,fastcgi buffer  size是否过小

 

4、 timeout PHP程序执行时间过长而超时    

     检查nginx和fastcgi中各种timeout设置。nginx 中的  fastcgi_connect_timeout 300;  

     fastcgi_send_timeout 300     fastcgi_read_timeout300       keepalive_timeout ;

     php-fpm中的request_terminate_timeout,php.ini中的max_execution_time

 

5、max_requests        php-fpm  

    该参数指明了每个children最多处理多少个请求后便会被关闭。在大量处理请求下,如果该值

    设置过小会导致 children频繁的自杀和建立 而浪费大量时间,若所有的children差不多都在这

    个时候自杀,则重建前将没有children响应请求,于是出现502。可以将该值设置大一些或者

    是0[无限]。

 

6、调高 linux 内核打开文件数量

    ( 必须是 root 帐号 )

     echo 'ulimit -HSn 65536'>> /etc/profile

     echo 'ulimit -HSn 65536'>> /etc/rc.local

     source /etc/profile

 

7、脚本执行时间超时

   如果脚本因为某种原因长时间等待不返回 ,导致新来的请求不能得到处理,调小如下配置。

   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



猜你喜欢

转载自blog.51cto.com/14124898/2405210