nginx 502
博客专区 > tystys 的博客 > 博客详情
nginx 502
tystys 发表于5个月前
nginx 502
  • 发表于 5个月前
  • 阅读 8
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

 

通常是由于前段服务器the frontend webserver--尝试连接 backend application handlers后端服务器 breakdown时

以下是执行过程|执行时又可能出现502|超时等

 

当然,配置错误也会爆502        

取决于不同的应用服务器--诊断502错误方案亦不同 

常见问题

PHP-FastCGI    (PHP-FPM)   NGINX.  

php-fpm未运行--502

php-fpm 无法启动

nginx无法和php-fpm通讯

nginx 超时

php-fpm超时 

PHP-FPM未运行

 ps +grep     查看是否运行php-fpm

 

root@nginx0:/var/log/nginx# ps aux | grep php
root     29191  0.0  1.7 133628 18108 ?        Ss   20:41   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 29193  0.0  0.5 133628  5956 ?        S    20:41   0:00 php-fpm: pool www
www-data 29194  0.0  0.5 133628  5956 ?        S    20:41   0:00 php-fpm: pool www

 

未运行|

则运行

init system command
SysV service php5-fpm start
Systemd systemctl start php5-fpm.service
init.d /etc/init.d/php5-fpm start
Upstart

initctl start php5-fpm

 

 

PHP-FPM启动失败 

如果无法启动--发起一个php页面的请求---tail nginx 错误日志 

www-data@nginx0:/var/log/nginx$ tail /var/log/nginx/error.log
2016/09/19 20:39:30 [crit] 28751#28751: *9 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.0.2.101, server: example.com, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "192.0.2.255"
2016/09/19 20:39:50 [crit] 28751#28751: *9 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.0.2.101, server: example.com, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "192.0.2.255"
2016/09/20 16:39:44 [crit] 28751#28751: *33 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.0.2.101, server: example.com, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "192.0.2.255"

  PHP-FPM 启动失败几乎百分百是配置问题

www-data@nginx0:/var/log/nginx$ tail /var/log/syslog
Sep 20 16:39:10 nginx0 kernel: [77167.468712] init: php5-fpm main process (3551) terminated with status 78
Sep 20 16:39:11 nginx0 kernel: [77167.468727] init: php5-fpm main process ended, respawning

 

www-data@nginx0:/var/log/nginx$ cat /usr/include/sysexits.h | grep 78
#define EX_CONFIG       78      /* configuration error */

  PHP-FPM配置错误

  PHP-FPM 配置问题

  fat fingers, or a genuine configuration conflict配置冲突.

首先确认保证php-fpm所在用户组对通讯目录是有写权限的.

Next, verify that the socket is being created with the appropriate user permissions.

In particular, check your worker pool configurations (in /etc/php5/fpm/pool.d/*.conf)

and verify:

  • socket path
  • process owner/group
  • listen.owner/listen.group
user = www-data
group = www-dat

listen = /var/run/php5-fpm.sock

listen.owner = www-data
listen.group = www-data

Notice the typo in the configuration file above: group = www-dat should read group = www-data.

Fixing this typo and restarting the PHP-FPM daemon resolves the issue.

NGINX 通讯PHP-FPM失败

 

如果PHP-FPM和你的application应用都没问题 are working properly, 那么问题可能出现在NGINX’s 配置上.  (/etc/nginx/sites-enabled/default, by default) and verify that the fastcgi_pass directive matches the socket (listen) in your PHP-FPM worker pool configuration.

[...]
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
                include fastcgi_params;

        }

配置更改后记得重启NGINX.

 

 

NGINX超时

如果后端应用正在运行,却出现了502,问题多数出现在正在请求|正在访问的应用上尤其应用没有设置时间限制,而nginx是有时间限制的,超出配置的时间,是不再等待的 

 不同的应用下,执行时间大于30秒的可能会被执行,这时就需要增加ngixn最大超时时间,

如果程序执行时间是异常导致的超时,就需要优化了, 

 配置最大超时时间--首先打开PHP-FPM’s configuration file (/etc/php5/fpm/php.ini),

找到

max_execution_time = 30

然后 打开/etc/nginx/nginx.conf),

添加如下内容到http模块

add the following within the http block to increase timeout windows, buffers, and buffer sizes:

http { 
...

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}

最后重启reload NGINX and PHP-FPM:

PHP-FPM:

init system command
SysV service php5-fpm reload
Systemd systemctl reload php5-fpm.service
init.d /etc/init.d/php5-fpm reload
Upstart initctl reload php5-fpm

NGINX:

init system command
SysV service nginx reload
Systemd systemctl reload nginx.service
init.d /etc/init.d/nginx reload
Upstart initctl reload nginx

PHP-FPM is timing out

有时,增加超时时间只是临时解决方案。

更多的应该是去找到你的应用程序,分析,优化,降低执行时间,  

例如,程序 在查询数据库时可能花费了大量的时间,直接超过了最大执行时间。  

如果nginx的执行时间足够高,临时解决方案是把php执行时间 set_time_limit()打开

当然 --长期的解决方案--还是去优化程序

 

共有 人打赏支持
粉丝 3
博文 72
码字总数 82272
×
tystys
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: