Nginx + Php-fpm 一个神奇的502错误

2018-09-06  本文已影响0人  _零

网上有各种各样的nginx 502解决错误,比如以下几种:

1.查看当前的PHP FastCGI进程数是否够用:

netstat -anpo | grep "php-cgi" | wc -l

2.增加nginx.conf配置文件中FastCGI的timeout时间:

......
http
{
......
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
......
}
......

等等。
但是大多数情况下我们并不会涉及到这些问题。比如刚才nginx+php-fpm还可以访问配置的域名,但是过一会突然的一个神奇的502错误,难道这些还是进程数不够?超时了?

但是网上有一句话说的很好:

Nginx 502错误的原因比较多,是因为在代理模式下后端服务器出现问题引起的。这些错误一般都不是nginx本身的问题,一定要从后端找原因!但nginx把这些出错都揽在自己身上了,着实让nginx的推广者备受置疑,毕竟从字眼上理解,bad gateway?不就是bad nginx吗?让不了解的人看到,会直接把责任推在nginx身上,希望nginx下一个版本会把出错提示写稍微友好一些,至少不会是现在简单的一句502 Bad Gateway,另外还不忘附上自己的大名。
(http://os.51cto.com/art/201011/233698.htm)

查看了一下nginx的错误日志,然而空空如也。抱着试试看的心态去找php-fpm的问题去了。

首先查看一下php-fpm的进程数:

netstat -napo |grep "php-fpm" | wc -l

what?竟然是0。为啥不是300,好定位到进程数不够这个问题上。好吧,既然是0,会不会是php-fpm没有启动的原因,我再试一试吧。

找到php-fpm的目录,默认安装的目录是

/usr/local/php/sbin/php-fpm

我的不是啦。然后php-fpm restart

Usage: php-fpm [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F [-O]]
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -h               This help
  -i               PHP information
  -m               Show compiled in modules
  -v               Version number
  -p, --prefix <dir>
                   Specify alternative prefix path to FastCGI process manager (default: /home/service/php).
  -g, --pid <file>
                   Specify the PID file location.
  -y, --fpm-config <file>
                   Specify alternative path to FastCGI process manager config file.
  -t, --test       Test FPM configuration and exit
  -D, --daemonize  force to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
                   force to stay in foreground, and ignore daemonize option from config file
  -O, --force-stderr
                   force output to stderr in nodaemonize even if stderr is not a TTY
  -R, --allow-to-run-as-root
                   Allow pool to run as root (disabled by default)

what?这又是些什么东西?

我还是先看看进程启动了没

ps -ef | grep PHP-fpm
root     17180 17102  0 15:56 pts/0    00:00:00 grep PHP-fpm

查看一下端口,看里面有没有php-fpm

netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6743/nginx
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      949/sshd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1029/master
tcp        0      0 :::22                       :::*                        LISTEN      949/sshd
tcp        0      0 ::1:25                      :::*                        LISTEN      1029/master

果然,没有启动

看命令帮助中有个参数-R
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)

运行了一下

php-fpm -R

然后接着运行

php-fpm
ERROR: unable to bind listening socket for address '9233': Address already in use (98)

初始化失败,显示端口号被占用,查看端口号

netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      17184/php-fpm
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6743/nginx
tcp        0      0 0.0.0.0:9233                0.0.0.0:*                   LISTEN      17184/php-fpm
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      949/sshd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1029/master
tcp        0      0 :::22                       :::*                        LISTEN      949/sshd
tcp        0      0 ::1:25                      :::*                        LISTEN      1029/master

然后在运行一下配置的域名,哇可以了。。。

最后说一下,以root全兴运行肯定会出各种各样的错误,这个也不例外,会收集一下相关文章。

持续更新中。

上一篇 下一篇

猜你喜欢

热点阅读