如何在我的本地NginX网站上解决此Bad Gateway错误? [英] How do I resolve this Bad Gateway error with my local NginX web site?
问题描述
我正在运行Ubuntu 16.10的笔记本电脑上运行本地测试服务器.我正在运行Apache2,但是我决定切换到NginX.我有五个站点,一个站点只是纯HTML,其他站点则是PHP 7.
仅HTML的网站就可以正常工作. PHP网站出现故障.最初,基于PHP的网站都返回了403 Forbidden
错误,如我之前提出的问题所述. /p>
但是,经过一些实验并查看了Web上的文档,看来问题可能出在每个站点的NginX配置文件都指向index.html
,而不是index.php
.因此,我从中更改了配置文件:
location / {
try_files $uri $uri/ /index.html;
}
...对此:
location / {
try_files $uri $uri/ /index.php;
}
不幸的是,那只会改变我得到的错误类型.现在,当我加载一个PHP网站时,出现一个502 Bad Gateway
错误,这就是/var/log/nginx/error.log
中显示的内容:
2017/01/07 17:28:26 [crit] 6308#6308: *9 connect() to unix:/var/run/php7.0-fpm.sock failed (2: No such file or directory) while connecti
ng to upstream, client: 127.0.0.1, server: local_examle.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7.0-fpm.
sock:", host: "local_example.com"
我以为我已经正确安装和配置了php7.0-fpm,但是我想NginX和PHP不能在我的系统上一起工作.
如何解决此Bad Gateway
错误,并使基于PHP的网站可以在NginX上正常工作?
为什么首先出现错误
由于nginx
正在将请求代理到不存在的端点,因此您收到错误消息.基本上,它永远不会到达PHP.这就是我们需要解决的问题.
告诉我们错误的地方在于:
[crit] 6308#6308: *9 connect() to unix:/var/run/php7.0-fpm.sock
我们正在采取的步骤
- 确保
php-fpm
有效 - 找出
php-fpm
在哪里接受连接的地方 - 修改nginx配置以反映上述内容
确保PHP-FPM正常运行
我正在做的是检查php-fpm
进程是否在进程列表中.这告诉我php-fpm
是否开始.
在您的终端中,输入:
ps aux | grep php
向您显示php进程列表.如果没有PHP-FPM,则表示它从未启动,因此您可以使用
启动它 sudo service php7.0-fpm start
.我以为php7.0-fpm
是服务的名称.
如果php-fpm
存在,则始终可以查看其已打开的文件描述符.这包括配置文件,库,网络连接和unix套接字.
首先,您需要找到php-fpm
进程的process ID
或PID. ps aux | grep php
的输出将为您提供一个列表,您想查看所有者为root
的进程(通常 说如果您使用默认值16.04).
PHP-FPM接受以下位置的连接
我们将使用sudo lsof -p 123456
列出打开的文件描述符,其中123456
是您从ps aux | grep php
获得的进程ID.
向下滚动列表,您正在寻找"unix"或"tcp"以查看其作用.
更改PHP-FPM在以下位置接受连接的地方
我们可以更改上述值,也可以保留.我之所以麻烦地展示这几个有用的命令,是为了强调您总是可以询问系统正在发生什么.
IF ,您知道配置 pool 的位置(通常/etc/php/7.0/fpm/pool.d/www.conf
然后打开它并找到listen
指令.更改或保留它,但使用nginx
的fastcgi_pass
值.
我写了很多东西,但是如果您继续使用Linux,那么最终您只会熟悉其中的一些命令.这没有什么太大的困难,所以我希望这不会使您摆脱这种服务PHP的方法.我会发布更多信息,但是目前我不在Linux机器附近,因此我没有发布终端输出示例.如果您在没有此答案的情况下不认为有用,我会在以后做.
祝你好运!
I am running a local testing server on my laptop running Ubuntu 16.10. I was running Apache2, but I've decided to switch over to NginX. I have five sites, one is just plain HTML, the others are PHP 7.
The site that is just HTML is working fine. The PHP sites are failing. At first, the PHP based sites were all returning a 403 Forbidden
error, as described in this question I asked earlier.
However, after some experiments and looking at documentation on the web, it seemed maybe the problem was that the NginX configuration files for each site were pointing to index.html
, not index.php
. So, I changed the configuration files from this:
location / {
try_files $uri $uri/ /index.html;
}
... to this:
location / {
try_files $uri $uri/ /index.php;
}
Unfortunately, that only changed the kind of error I get. Now, when I load one of my PHP sites, I get a 502 Bad Gateway
error, and this is what shows up in /var/log/nginx/error.log
:
2017/01/07 17:28:26 [crit] 6308#6308: *9 connect() to unix:/var/run/php7.0-fpm.sock failed (2: No such file or directory) while connecti
ng to upstream, client: 127.0.0.1, server: local_examle.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7.0-fpm.
sock:", host: "local_example.com"
I thought I had php7.0-fpm installed and configured correctly, but I guess somehow NginX and PHP are not working together on my system.
How do I resolve this Bad Gateway
error and get my PHP based web sites to work on NginX?
Why do you get the error in the first place
You're getting the error because nginx
is proxying request to a nonexistent endpoint. Basically, it never reaches PHP. That's what we need to fix.
What tells us the error lies in that is this:
[crit] 6308#6308: *9 connect() to unix:/var/run/php7.0-fpm.sock
Steps we're taking
- ensuring
php-fpm
works - finding out where
php-fpm
is accepting connections at - modifying nginx config to reflect the above
Ensuring PHP-FPM works
What I do is checking whether php-fpm
process is in the process list. That tells me whether php-fpm
started.
In your terminal, type:
ps aux | grep php
That shows you the list of php processes. If PHP-FPM isn't there, it means it never started so you can start it with
sudo service php7.0-fpm start
. I assumed that php7.0-fpm
is the name of the service.
If php-fpm
IS there, you can always look at file descriptors it has open. This includes config files, libraries, network connections and unix sockets.
First, you need to find the process ID
or PID of your php-fpm
process. Output of ps aux | grep php
will give you a list, you want to look at the process whose owner is root
(it usually says that if you used defaults for 16.04).
Where PHP-FPM is accepting connections at
We'll list open file descriptors using sudo lsof -p 123456
where 123456
is the process ID you obtained from ps aux | grep php
.
Scroll down the list, you're looking for "unix" or "tcp" to see what it does.
Changing where PHP-FPM is accepting connections at
We can change the above value or you can keep it. Reason I went through the trouble to show these few useful commands was to highlight that you can always ask your system what's going on.
IF you know where config file is for your pool (usually /etc/php/7.0/fpm/pool.d/www.conf
then open it and find listen
directive. Change that or keep it, but use it in nginx
's fastcgi_pass
value.
I wrote a lot, but it's only a few commands which you'll get familiar eventually if you keep using Linux. It's nothing overly difficult so I hope this won't turn you away from this method of serving PHP. I'd post more info but I'm not near a Linux machine at this moment so I didn't post terminal outputs for examples. I'll do it later if you don't find this answer useful without it.
Good luck!
这篇关于如何在我的本地NginX网站上解决此Bad Gateway错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!