天天看点

nginx的502问题

1.配置错误

因为nginx找不到php-fpm了或者socket文件权限不对,所以报错,一般是nginx的配置文件中fastcgi_pass后面的路径配置不当,后面可以是socket或者是ip:port,从php5.4版本之后如果使用socket方式,默认权限给的比较低,所以需要把监听权限改为777,对应配置为 listen.mode=0777

2.资源耗尽

lnmp架构在处理php时,nginx直接调取后端的php-fpm服务,如果nginx的请求量偏高,我们又没有给php-fpm配置足够的子进程,那么php-fpm就会资源耗尽,一旦资源耗尽nginx找不到php-fpm就会出现502错误

解决方案:

去调整php-fpm.conf中的pm.max_children数值,使其增加,但是也不能无限增加,毕竟资源有限,一般4G内存机器如果跑php-fpm和nginx,不跑mysql可以设置为150,8G为300以此类推!

3.除了上面的两种错误还有其他的原因很少有,我们可以借助nginx的错误日志来进行排查

vim /usr/local/nginx/logs/nginx_error.log  我们也可以给日志定义级别vim/usr/local/nginx/conf/nginx.conf 找到error_log,默认是crit最严谨的就行,也可以改成debug显示的信息最全面,但是很容易撑爆我们的磁盘。

如下为:nginx解析php相关配置

<code>[root@chy01 vhost]</code><code># vim test.com.conf </code>

<code>   </code><code>location ~ \.php$</code>

<code>            </code><code>{</code>

<code>            </code><code>include fastcgi_params;</code>

<code>            </code><code>fastcgi_pass unix:</code><code>/tmp/php-fcgi</code><code>.sock;</code>

<code>            </code><code>fastcgi_index index.php;</code>

<code>            </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/data/wwwroot/test</code><code>.com/$fastcgi_script_name;</code>

<code>       </code><code>}</code>

<code>(如上在虚拟主机里面配置的php解析,需要注意几个地方1fastcgi_pass unix:</code><code>/tmp/php-fcgi</code><code>.sock; 这里的路径不要写错,如果写错会出现502问题,</code>

<code>[root@chy01 conf]</code><code># cat /usr/local/php-fpm/etc/php-fpm.conf</code>

<code>[global]</code>

<code>pid = </code><code>/usr/local/php-fpm/var/run/php-fpm</code><code>.pid</code>

<code>error_log = </code><code>/usr/local/php-fpm/var/log/php-fpm</code><code>.log</code>

<code>[www]</code>

<code>listen = </code><code>/tmp/php-fcgi</code><code>.sock (这个路径要与nginx虚拟主机的路径完全保持一致)</code>

<code>listen.mode = 666</code>

<code>user = php-fpm</code>

<code>group = php-fpm</code>

<code>pm = dynamic</code>

<code>pm.max_children = 50</code>

<code>pm.start_servers = 20</code>

<code>pm.min_spare_servers = 5</code>

<code>pm.max_spare_servers = 35</code>

<code>pm.max_requests = 500</code>

<code>rlimit_files = 1024</code>

<code>还有另一种情况就是php监听的不是sock,而是ip加端口的情况时   </code>

<code>location ~ \.php$</code>

<code>            </code><code>{ </code>

<code>            </code><code>#fastcgi_pass unix:/tmp/php-fcgi.sock;</code>

<code>            </code><code>fastcgi_pass 127.0.0.1:9000</code>

<code>       </code><code>}    </code>

<code>这时fastcgi_pass 127.0.0.1:9000 这里就需要这样操作,这的ip要与php配置文件里面监听的地址保持一致)</code>

<code>第2个需要注意的地方:fastcgi_param SCRIPT_FILENAME </code><code>/data/wwwroot/test</code><code>.com/$fastcgi_script_name; 这里的路径要与server 下的</code>

<code>{</code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name </code><code>test</code><code>.com test2.com lll.com;</code>

<code>    </code><code>index index.html index.htm index.php admin.php;</code>

<code>    </code><code>root </code><code>/data/wwwroot/test</code><code>.com; 这个路径保持一致。</code>

<code>    </code><code>if</code> <code>($host != </code><code>'test.com'</code> <code>) {</code>

<code>       </code><code>rewrite  ^/(.*)$  http:</code><code>//test</code><code>.com/$1  permanent;</code>

<code>    </code><code>}</code>

<code>3.php5.4之后的版本有个特性:</code>

<code>[root@chy01 vhost]</code><code># vi /usr/local/php-fpm/etc/php-fpm.conf (在配置文件中)</code>

<code>listen = </code><code>/tmp/php-fcgi</code><code>.sock</code>

<code>(如果监听的是sock就必须要监听mode)</code>

<code>#listen.mode = 666</code>

<code>[root@chy01 vhost]</code><code># ls -l /tmp/php-fcgi.sock </code>

<code>srw-rw---- 1 root root 0 8月  15 03:02 </code><code>/tmp/php-fcgi</code><code>.sock</code>

<code>(可以看到属主属组都为root,并且没有读的权限)</code>

<code>[root@chy01 vhost]</code><code># curl -x127.0.0.1:80 test.com/admin.php -I</code>

<code>HTTP</code><code>/1</code><code>.1 502 Bad Gateway</code>

<code>Server: nginx</code><code>/1</code><code>.12.1</code>

<code>Date: Mon, 14 Aug 2017 19:03:48 GMT</code>

<code>Content-Type: text</code><code>/html</code>

<code>Content-Length: 173</code>

<code>Connection: keep-alive</code>

<code>(当在curl时会发现报502)</code>

<code>[root@chy01 vhost]</code><code># tail -n3 /usr/local/nginx/logs/nginx_error.log </code>

<code>2017</code><code>/08/14</code> <code>22:24:06 [crit] 3684</code><code>#0: *7 connect() to unix:/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/php-fcgi.sock:", host: "test.com"</code>

<code>2017</code><code>/08/15</code> <code>03:03:48 [crit] 3682</code><code>#0: *5 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"</code>

<code>2017</code><code>/08/15</code> <code>03:06:03 [crit] 3682</code><code>#0: *7 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"</code>

<code>(查看错误日志,Permission denied发现拒绝,没有权限。是因为它的属主,属组为nobody ,此时如果将</code><code>/tmp/php-fcgi</code><code>.sock 的权限设置为nobody就没有任何问题了)</code>

<code>[root@chy01 vhost]</code><code># ps aux |grep nginx</code>

<code>root       3485  0.0  0.1  21272  1652 ?        Ss   02:28   0:00 nginx: master process </code><code>/usr/local/nginx/sbin/nginx</code> <code>-c </code><code>/usr/local/nginx/conf/nginx</code><code>.conf</code>

<code>nobody     3682  0.0  0.2  23152  3796 ?        S    03:01   0:00 nginx: worker process</code>

<code>nobody     3683  0.0  0.2  23152  3300 ?        S    03:01   0:00 nginx: worker process</code>

<code>root       3791  0.0  0.0 112664   972 pts</code><code>/0</code>    <code>S+   03:11   0:00 </code><code>grep</code> <code>--color=auto nginx</code>

<code>(还有一种情况也是502就是nginx的资源耗尽))</code>

nginx的502问题

     本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1980650,如需转载请自行联系原作者