php nginx 502 网关错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4252368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:24:31  来源:igfitidea点击:

nginx 502 bad gateway

phpnginxfastcgi

提问by Joel Wickard

I get a 502 Bad Gateway with nginx when using spawn fcgi to spawn php5-cgi.

使用 spawn fcgi 生成 php5-cgi 时,我使用 nginx 得到 502 Bad Gateway。

I use this to span an instance on server start using the following line in rc.local

我使用它在 rc.local 中使用以下行跨服务器启动实例

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

presumably I'm getting the error because the spawn-fcgi / php5-cgi dies and there is nothing listening there anymore to parse php.

大概我收到错误是因为 spawn-fcgi / php5-cgi 死了并且没有任何东西在那里听来解析 php。

I get nothing in the logs that I can see anywhere, I'm out of ideas (and new to this setup with nginx)

我在任何地方都看不到日志中的任何内容,我没有想法(并且是使用 nginx 进行此设置的新手)

回答by fadil

I executed my localhostand the page displayed the 502 bad gatewaymessage. This helped me:

我执行了 mylocalhost并且页面显示了502 bad gateway消息。这帮助了我:

  1. Edit /etc/php5/fpm/pool.d/www.conf
  2. Change listen = /var/run/php5-fpm.sockto listen = 127.0.0.1:9000
  3. Ensure the location is set properly in nginx.conf.
  4. Run sudo service php5-fpm restart
  1. 编辑 /etc/php5/fpm/pool.d/www.conf
  2. 更改listen = /var/run/php5-fpm.socklisten = 127.0.0.1:9000
  3. 确保在 nginx.conf 中正确设置了位置。
  4. sudo service php5-fpm restart

Maybe it will help you.

也许它会帮助你。

Source from: http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm

来源:http: //wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm

回答by sdolgy

The 502 error appears because nginx cannot hand off to php5-cgi. You can try reconfiguring php5-cgi to use unix sockets as opposed to tcp .. then adjust the server config to point to the socket instead of the tcp ...

出现 502 错误是因为 nginx 无法移交给 php5-cgi。您可以尝试重新配置 php5-cgi 以使用 unix 套接字而不是 tcp .. 然后调整服务器配置以指向套接字而不是 tcp ...

ps auxww | grep php5-cgi #-- is the process running?  
netstat -an | grep 9000 # is the port open? 

回答by techvineet

Go to /etc/php5/fpm/pool.d/www.confand if you are using sockets or this line is uncommented

转到/etc/php5/fpm/pool.d/www.conf,如果您正在使用套接字或此行未注释

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

Set couple of other values too:-

也设置几个其他值:-

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

Don't forget to restart php-fpm and nginx. Make sure you are using the same nginx owner and group name.

不要忘记重新启动 php-fpm 和 nginx。确保您使用相同的 nginx 所有者和组名。

回答by Ken Prince

You have to match the settings for PHP-FPM and Nginx to communicate over sockets or TCP.

您必须匹配 PHP-FPM 和 Nginx 的设置才能通过套接字或 TCP 进行通信。

So go to /etc/php5/fpm/pool.d/www.confand look for this line:

所以去/etc/php5/fpm/pool.d/www.conf寻找这一行:

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

Then go to /etc/nginx/nginx.conf

然后去 /etc/nginx/nginx.conf

Look for this:

寻找这个:

upstream php {
    server unix:/var/run/php5-fpm.socket;
}

Match those values and you should be all set.

匹配这些值,您应该已准备就绪。

回答by tjb

If running a linux server, make sure that your IPTABLES configuration is correct.

如果运行 linux 服务器,请确保您的 IPTABLES 配置正确。

Execute sudo iptables -L -n, you will recieve a listing of your open ports. If there is not an Iptables Rule to open the port serving the fcgi script you will receive a 502 error. The Iptables Rule which opens the correct port must be listed beforeany rule which categorically rejects all packets (i.e. a rule of the form "REJECT ALL -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachableor similar)

执行sudo iptables -L -n,您将收到您的开放端口列表。如果没有 Iptables 规则来打开为 fcgi 脚本提供服务的端口,您将收到 502 错误。打开正确端口的 Iptables 规则必须列任何明确拒绝所有数据包的规则之前(即形式"REJECT ALL -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable或类似的规则)

On my configuration, to properly open the port, I had to execute this command (assume my fcgi server is running at port 4567):

在我的配置中,要正确打开端口,我必须执行此命令(假设我的 fcgi 服务器在端口 4567 上运行):

sudo iptables -I INPUT 1 -p tcp --dport 4567 -j ACCEPT

WARNING: This will open port 4567 to the whole world.

警告:这将向全世界开放端口 4567。

So it might be better to do something like this:

所以做这样的事情可能会更好:

   sudo iptables-save >> backup.iptables
   sudo iptables -D INPUT 1 #Delete the previously entered rule
   sudo iptables -I INPUT 1 -p tcp --dport 8080 -s localhost -j ACCEPT # Add new rule

Doing this removed the 502 error for me.

这样做为我消除了 502 错误。

回答by user2816137

change

改变

fastcgi_pass    unix:/var/run/php-fpm.sock;

to

fastcgi_pass    unix:/var/run/php5-fpm.sock;

回答by Meekohi

You can make nginx ignore client aborts using:

您可以使用以下方法使 nginx 忽略客户端中止:

location / {
  proxy_ignore_client_abort on;
}

回答by Ali Haris

I had the same problem while setting up an Ubuntu server. Turns out I was having the problem due to incorrect permissions on socket file.

我在设置 Ubuntu 服务器时遇到了同样的问题。原来我遇到了问题,因为套接字文件的权限不正确。

If you are having the problem due to a permission problem, you can uncomment the following lines from: /etc/php5/fpm/pool.d/www.conf

如果您由于权限问题而遇到问题,您可以取消注释以下行:/etc/php5/fpm/pool.d/www.conf

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

Alternatively, although I wouldn't recommend, you can give read and write permissions to all groups by using the following command.

或者,虽然我不推荐,但您可以使用以下命令为所有组授予读写权限。

sudo chmod go+rw /var/run/php5-fpm.sock

回答by neubert

When I did sudo /etc/init.d/php-fpm startI got the following error:

当我这样做时,sudo /etc/init.d/php-fpm start我收到以下错误:

Starting php-fpm: [28-Mar-2013 16:18:16] ERROR: [pool www] cannot get uid for user 'apache'

I guess /etc/php-fpm.d/www.confneeds to know the user that the webserver is running as and assumes it's apache when, for nginx, it's actually nginx, and needs to be changed.

我想/etc/php-fpm.d/www.conf需要知道网络服务器正在运行的用户,并假设它是 apache,而对于 nginx,它实际上是 nginx,并且需要更改。

回答by h0tw1r3

Try disabling the xcache or apc modules. Seems to cause a problem with some versions are saving objects to a session variable.

尝试禁用 xcache 或 apc 模块。似乎导致某些版本将对象保存到会话变量的问题。