Home  >  Article  >  Backend Development  >  Nginx reverse proxy port domain name cannot be accessed problem solved

Nginx reverse proxy port domain name cannot be accessed problem solved

WBOY
WBOYOriginal
2016-08-08 09:29:203799browse

I solved this problem on the first morning of work in the new year. To be honest, it was the first time I encountered it. Using a domain name to proxy a web project, when requesting, the domain name is not used for some reason, but the IP port is directly used as the path, causing the web page to crash and become unusable.

Because nginx is used as the front-end server, I made some modifications. The following is the comparison before and after the modification:

Before modification:

server {
                        80;                                                                                                                          .aaaaa " proxy_set_header X-Real-IP $remote_addr;
index index.html index .htm ; }


Modified: server {                           80; server_name xxx.aaaaa.com; location / { proxy_pass http://10.148.2
2
.8
1
:8180;
proxy_read_timeout 600s;
                                                              proxy_set_header      $host:$server_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                   index   index.html index.htm; 02 503 504 /50x.html;
location = /50x.html {
root html;

}

} 访 The visit is successful.


Of course it is recommended that for static resources, use for example
location ~ .*.(gif|jpg|png|html|htm|css|js|flv|ico|swf)(.*) {                   proxy_redirect off;             proxy_set_header Host $host;             proxy_cache cache_one;             proxy_cache_valid 200 302 1h;
            proxy_cache_valid any 1m;
              expires 30d;
                          ’ ’                 ’ ’s ’ ’ s ’ through ’ ‐ ‐ ‐‐ ‐‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ 1m;



The main reference is the original text. I am very grateful to this big brother for his guidance:


http://www.cnblogs.com/likehua/p/4056625.html

The content is as follows:

The default reverse port of Nginx is 80, so there is a problem that the port after being proxied is 80, which leads to access errors. The main reason is that the response port is not set in the host configuration of the Nginx configuration file.

The relevant configuration files are as follows:







1
2

34

proxy_pass http://ime-server/ime-server;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

As above, the Host configuration only has host and no corresponding port, which results in obtaining the wrong port at the place being proxied. This article takes java as an example:

1

2

3

4

5

String scheme = httpRequest.getScheme();

String serverName = httpRequest.getServerName();

int port = httpRequest.getServerPort();

​​​//Service request address

​​​​String requestURI = scheme+"://"+serverName+":"+port+"/ime-server/rest/"+serviceName+"/wmts";

At this time, the obtained port is 80, although the port nginx listens to is 9090. This error makes me very depressed. Therefore, modify the nginx configuration file and change the value after Host to $host:$server_port. The configuration file is as follows:

1

2

3

4

5

6

7

8

location /ime-server {

                                           html;

                                                     index.html index.htm;

                                                http:

//ime-server/ime-server;

                               Host $host:$server_port;

                                               X-Real-IP $remote_addr;

    

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    

}

Restart nginx,./nginx -s reload. Then check whether the port information after being proxied is correct:

The above introduces the solution to the problem that the Nginx reverse proxy port domain name cannot be accessed, including the relevant aspects. 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