How do I resolve this Bad Gateway error with my lo

2019-09-08 02:42发布

问题:

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?

回答1:

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!



标签: php nginx