当前位置:首页 > 服务器技术 > nginx

Nginx反向代理端口域名无法访问问题解决

开年上班第一天早上解决这个问题,说实在的还是第一次碰到。使用域名代理web项目,请求时候,不知为何不使用域名,而是直接ip端口作为路径,导致网页崩溃无法使用。

因为使用nginx作为前端服务器,所以稍微做了一下修改,下面为修改前和修改后的对比:

修改前:

server {
listen 80;
server_name
xxx.aaaaa.com;
location / {
proxy_pass http://10.148.22.81:8180;
proxy_set_header X-Real-IP $remote_addr;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

修改后:

server {
listen 80;
server_name xxx.aaaaa.com;
location / {
proxy_pass http://10.1
48.22.81:8180;
proxy_read_timeout 600s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

访问成功。

当然建议对于静态资源,使用例如

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 301 1d;
proxy_cache_valid any 1m;
expires 30d;
index index.html index.htm;
}
效率要好的。

主要参考原文为,很感谢这位大哥指导:

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

内容如下:

Nginx默认反向后的端口为80,因此存在被代理后的端口为80的问题,这就导致访问出错。主要原因在Nginx的配置文件的host配置时没有设置响应的端口。

相关配置文件如下:

<table border="0" cellpadding="0" cellspacing="0"Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important"><tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important">

1

2

3

4

<tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_pass http:<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(0,130,0)!important; background:none!important">//ime-server/ime-server;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header Host $host;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header X-Real-IP $remote_addr;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

如上,Host配置只有host,没有对应的port,这就导致在被代理的地方取得错误的端口。本文以java为例:

<table border="0" cellpadding="0" cellspacing="0"Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important"><tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important">

1

2

3

4

5

<tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">String scheme = httpRequest.getScheme();

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">String serverName = httpRequest.getServerName();

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(0,0,255)!important; background:none!important">int <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">port = httpRequest.getServerPort();

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(0,130,0)!important; background:none!important">//服务请求地址

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">String requestURI = scheme+<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:blue!important; background:none!important">"://"<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">+serverName+<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:blue!important; background:none!important">":"<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">+port+<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:blue!important; background:none!important">"/ime-server/rest/"<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">+serviceName+<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:blue!important; background:none!important">"/wmts"<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">;

  这时,取得的port为80,虽然nginx监听的端口为9090。这个错误让我很郁闷。于是,修改nginx的配置文件,将Host后面的改为 $host:$server_port即可,配置文件如下:

<table border="0" cellpadding="0" cellspacing="0"Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important"><tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important">

1

2

3

4

5

6

7

8

<tdBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">location /ime-server {

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">#root html;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">#index index.html index.htm;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_pass http:<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; color:rgb(0,130,0)!important; background:none!important">//ime-server/ime-server;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header Host $host:$server_port;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header X-Real-IP $remote_addr;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

<codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important"> <codeBitstream Vera Sans Mono','Courier New',Courier,monospace!important; min-height:inherit!important; background:none!important">}

  重启nginx,./nginx -s reload 。然后检查被代理后的端口信息是否正确:

以上就介绍了Nginx反向代理端口域名无法访问问题解决,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!