502错误的网关

时间:2016-08-27 20:05:26

标签: nginx fpm

我遇到的问题往往是,如果我去我的服务器上使用php的页面,我会得到" 502 Bad Gateway"错误。

错误日志:

/var/log/nginx/error.log每分钟显示此错误的大约3个副本:

2016/08/27 15:07:22 [error] 17309#0: *53554 connect() to unix:/var/run/php5-fpm.sock 
failed (11: Resource temporarily unavailable) while connecting to upstream, client: 
[dedicated server], server: localhost, request: "POST /xmlrpc.php HTTP/1.0", 
upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: [my IP address]

来自nginxphp5-fpm进程的CPU负载有时很大(超过100%),但有时只是显着(2%),很少无关紧要。

这是我在syslog(!)中看到的很多东西:

Aug 27 15:17:21 [site] avahi-daemon[871]: Invalid response packet from host 
[some IP address that isn't mine and nslookup never heard of].

到目前为止我尝试过的事情:

  • apt-get update
  • 已移除并重新安装php5php5-cgiphp5-fpm
  • 确保apache2未在我的系统上运行
  • added 这个nginx.conf位于http { ... }

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

  • 确保运行nginx的同一用户拥有/var/run/php5-fpm.sock。这是listen.ownerlisten.group/etc/php5/fpm/pool.d/www.conf引用的所有者。

  • 尝试将对该套接字的引用更改为TCP / IP套接字:

    /etc/nginx/sites-available/default包含第fastcgi_pass unix:127.0.0.1:9000;/etc/php5/fpm/pool.d/www.conf包含行listen = 127.0.0.1:9000;

    由于这使得没有任何php页面能够正常工作,我还是恢复了这一改变。

  • 确保我未在nginx.conf设置中禁用PHP文件。我没有,有时也有效,所以不可能。

php5-fpm设置:

我怀疑这是一个问题,因为(a)它经常会这样做,而且(b)如果我没有得到504错误,我会得到一个非常慢的加载时间。任何使用php的页面。以下是我认为/etc/php5/fpm/pool.d/www.conf文件的相关部分:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

那么:我还应该尝试什么? TIA。

1 个答案:

答案 0 :(得分:5)

You can check following things,

1- sudo service php5-fpm status, try to start that if no running

2- while changing socket to tcp/ip remove unix, ie, in the /etc/nginx/sites-available/default file, in the php block change fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass 127.0.0.1:9000; and in /etc/php5/fpm/pool.d/www.conf file change listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000

3- try to increase the process managers for php, i.e., pm.max_children=40, pm.start_servers = 10, pm.min_spare_servers = 5, pm.max_spare_servers = 10

4- switch off keepalive connections from nginx.conf

5- try to implement caching (is done for almost static content, so that all requests do not bother php) if possible.