Home  >  Article  >  php教程  >  Remember to troubleshoot the 502 bad gateway error when nginx deploys the yii2 project

Remember to troubleshoot the 502 bad gateway error when nginx deploys the yii2 project

WBOY
WBOYOriginal
2016-12-05 13:26:271937browse

I had nothing to do on Saturday, so I tried to install and deploy yii2. There was no problem during the installation process, but when I deployed it to nginx, I encountered a 502 bad gateway problem. It took me a long time to fix it. This problem is something I have not encountered before when deploying yii2, so I note it here for reference.

1. Installation and deployment environment

Operating system: macOS, php version: 5.6, nginx version: 1.10.1, yii2 version: 2.0.

2, installation of yii2

The installation of yii2 is very simple, just refer to the manual on the official website. What I installed here is yii2-app-advanced (Yii 2 Advanced Project Template). The project address is on github. Just follow the installation instructions in the README step by step. I won’t go into details about this.

3, yii2 deployment on nginx

In fact, the README of the yii2-app-advanced project has made it very clear on how to deploy on nginx. There should be no problems if you follow the steps. But I was lazy during deployment and directly used the nginx deployment parameters in the README, which resulted in a 502 bad gateway error.

The response codes starting with 5 in the HTTP status code are all caused by the server side, so take a look at the nginx log:

error log:

When I saw upstream: "fastcgi: //127.0.0.1:9090", the port seemed wrong, so I checked the configuration of the virtual host:

Let’s take a look at the configuration of php-fpm:

Sure enough, the FastCGI server listening port specified by the nginx virtual host is wrong, which will cause nginx to not get a response from the FastCGI server. So when the browser makes a request, nginx returns a 502 status code to the browser, telling the browser that the upstream FastCGI server has not responded.

4, solved

In the nginx virtual host, modify the port specified by fastcgi_pass correctly, and this error will be solved.

5, summary

(1) Errors starting with HTTP status code 5 are caused by the server side. 502 bad gateway is caused by the server (web server) working as a gateway or proxy trying to execute a request. It receives an invalid message from the upstream server (fastcgi server). the response to.

(2) fastcgi_pass is a configuration directive of the ngx_http_fastcgi_module module. It specifies the address of the fastcgi server. Its description in the nginx document is as follows:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">Syntax:    fastcgi_pass address;
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">Default</span><span style="color: #000000;">:    —
</span><span style="color: #008080;"> 3</span> Context:    location, <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">in</span><span style="color: #000000;"> location
</span><span style="color: #008080;"> 4</span> 
<span style="color: #008080;"> 5</span> <span style="color: #000000;">Sets the address of a FastCGI server. The address can be specified as a domain name or IP address, and a port:
</span><span style="color: #008080;"> 6</span> fastcgi_pass localhost:9000<span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span> 
<span style="color: #008080;"> 8</span> or as a UNIX-<span style="color: #000000;">domain socket path:
</span><span style="color: #008080;"> 9</span> fastcgi_pass unix:/tmp/<span style="color: #000000;">fastcgi.socket;
</span><span style="color: #008080;">10</span> 
<span style="color: #008080;">11</span> <span style="color: #0000ff;">If</span> a domain name resolves to several addresses, all of them will be used <span style="color: #0000ff;">in</span> a round-robin fashion. <span style="color: #0000ff;">In</span> addition, an address can be specified as a server group.

Reference:

HTTP status code

fastcgi_pass

Syntax: <strong>fastcgi_pass</strong> <code><em>address</em>;
Default:
Context: location, if in location
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn