Home  >  Article  >  Backend Development  >  The nginx page is not fully loaded or prompts 502bad gateway, and the nginx reverse proxy port number is missing. What does bad gateway mean? LeTV 502 bad gateway dnf 502 bad gateway.

The nginx page is not fully loaded or prompts 502bad gateway, and the nginx reverse proxy port number is missing. What does bad gateway mean? LeTV 502 bad gateway dnf 502 bad gateway.

WBOY
WBOYOriginal
2016-07-29 08:53:571994browse

In Nginx reverse proxy mode, the page may not be fully loaded, or a 502 bad gateway may occur directly.

There are many situations where 502 bad gateway occurs, mostly due to some nginx related timeout setting issues. The following discusses a situation that is relatively rare but must be paid attention to.

Appearance environment

nginx works in reverse proxy mode, listening to non-80 ports (this is very important, listening to non-80 ports often means that the user is preparing to configure multiple virtual hosts, but is not limited to this situation), in the form of IP Access (should also exist under domain name access, this has not been verified).

Specific problem

When accessing a page containing multiple elements (referring to not just static HTML pages, the client often requests multiple elements from the server, such as images, css formats, etc.), the page accessed by the client is incomplete , lacks elements such as image styles, and returns a 502 bad gateway error.

Problem Analysis

When I first encountered this problem, I thought it was because the host field value was illegal and was blocked by the back-end server. The reason for thinking this was as follows:
1. Assume that the nginx listening address is 1.1.1.1:2001, the proxy address is 2.2.2.2, and the proxy server is configured in upstream, as follows:
listen 1.1.1.1:2001;

upstream backend1 {
server 2.2.2.2;
}

proxy_set_header Host $host;
proxy_pass http://backend1;
2. Under the above configuration, capture packets through wireshark, and the client accesses the nginx proxy, that is, when 1.1.1.1:2001, the requested host is, Host:1.1.1.1. If proxy_set_header is not set, nginx accesses the host of the back-end server. For, Host:backend1

(Nginx official documentation mentions: Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

If proxy_set_header Host $host is set, the host used by nginx to access the backend server is 1.1.1.1. In either case, if the backend server restricts the host field of the request packet for security reasons (only 2.2.2.2, that is, the address of the server itself is reasonable), then all incorrect access to the host field will be denied or repeated. If the redirection or other processing is done, normal access will not be possible, so when encountering this problem, I began to think that the back-end server had imposed restrictions.
3. After thinking about it, if the back-end server really restricts the host field of the access package, there should be no incomplete page loading. After all, few servers restrict certain elements but not others. Through further packet capture analysis, after the client accesses a page containing multiple elements for the first time, the server will tell the client the addresses of other elements contained in the page, so that the client can continue to request to obtain a web page with complete information, and the server tells The ip:port of the address of other elements of the client is exactly the same as the host field used by nginx to communicate with the server (it is understandable that nginx acts as a reverse proxy). If the port is not set in the host, the default is 80 (this is why nginx often listens to port 80 No problem), the client will obtain the page elements based on this returned address.
4. So in the above situation, if proxy_set_header Host $host is set, the address for the client to access other elements of the page will be http://1.1.1.1/…. You can see that it is still through the nginx proxy, but here comes the problem: nginx The listening port is 1.1.1.1:2001, and the new access is 1.1.1.1:80 (http port defaults to 80, https defaults to 443), so the request cannot be proxy through nginx.

Solution

The solution is also very simple, which is proxy_set_header Host $host:$server_port, which will add the port that nginx listens to.


Postscript

Although this time it turns out that the access is not affected by the server's restrictions on the Host field, I personally think that the restriction of the host field is worth noting. Nginx officials also recommend that access to illegal hosts be directly denied. of.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the nginx page that is not fully loaded or prompts 502 bad gateway, and the nginx reverse proxy port number is missing, including the content of bad gateway. I hope it will be helpful to friends who are interested in PHP tutorials.

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