Troubleshoot the 502 bad running wary error when deploying the yii2 project in nginx. nginxyii2
When I had nothing to worry about on Saturday, I tried to install and deploy yii2. There was no problem with the installation process. However, when I deployed it to nginx, I encountered the 502 bad wary problem. I had to wait for half a day. This problem was not encountered before I deployed yii2, so remember to note it here.
1. Install and deploy the environment
Operating System: macOS, php version: 5.6, nginx version: 1.10.1, yii2 version: 2.0.
2. Install yii2
Yii2 is easy to install. Please refer to the Manual on the official website. I installed the yii2-app-advanced here (Yii 2 Advanced Project Template), the Project address on github, according to the installation instructions in README step by step, this is not much to say.
3. Deployment of yii2 on nginx
In fact, the yii2-app-advanced project README has been very clear about how to deploy on nginx, follow the steps should be no problem. However, I was a little lazy during deployment and used the deployment parameters of nginx in README directly. As a result, the 502 bad gateway error was caused.
The HTTP status code with 5 headers is caused by the server, so check the nginx log:
Error log:
See upstream: "fastcgi: // 127.0.0.1: 9090". Here, the port seems to be incorrect, so check the configuration of the VM:
Let's take a look at the configuration of php-fpm:
Indeed, the FastCGI server listening port specified by the nginx virtual host is incorrect, which will cause nginx to fail to receive the response from the FastCGI server. Therefore, when the browser sends a request, nginx returns a 502 status code to the browser, telling the browser that the upstream FastCGI server has no response.
4. Solution
In the nginx virtual host, correct the port specified by fastcgi_pass. This error is fixed.
5. Summary
(1) HTTP Status Code 5 headers are caused by the server side, and 502 bad gateway is caused by the attempt to execute the request as a gateway or proxy server (web Server, the upstream server (fastcgi server) receives invalid responses.
(2) fastcgi_pass is a configuration instruction of the ngx_http_fastcgi_module module, which specifies the address of the fastcgi server. The nginx document describes it as follows:
1 Syntax: fastcgi_pass address; 2 Default: — 3 Context: location, if in location 4 5 Sets the address of a FastCGI server. The address can be specified as a domain name or IP address, and a port: 6 fastcgi_pass localhost:9000; 7 8 or as a UNIX-domain socket path: 9 fastcgi_pass unix:/tmp/fastcgi.socket;10 11 If a domain name resolves to several addresses, all of them will be used in a round-robin fashion. In addition, an address can be specified as a server group.
Refer:
HTTP status code
Fastcgi_pass
Syntax: |
fastcgi_pass address ; |
Default: |
- |
Context: |
location ,if in location |