Nginx反向代理关于端口的问题
http://www.cnblogs.com/likehua/p/4056625.html
Nginx默认反向后的端口为80,因此存在被代理后的端口为80的问题,这就导致访问出错。主要原因在Nginx的配置文件的host配置时没有设置响应的端口。
相关配置文件如下:
1
2
3
4
|
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;
|
如上,Host配置只有host,没有对应的port,这就导致在被代理的地方取得错误的端口。本文以java为例:
1
2
3
4
5
|
String scheme = httpRequest.getScheme();
String serverName = httpRequest.getServerName();
int port = httpRequest.getServerPort();
//服务请求地址
String requestURI = scheme+ "://" +serverName+ ":" +port+ "/ime-server/rest/" +serviceName+ "/wmts" ;
|
这时,取得的port为80,虽然nginx监听的端口为9090。这个错误让我很郁闷。于是,修改nginx的配置文件,将Host后面的改为 $host:$server_port即可,配置文件如下:
1
2
3
4
5
6
7
8
|
location /ime-server {
#root html;
#index index.html index.htm;
proxy_pass http: //ime-server/ime-server;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
|
重启nginx,./nginx -s reload 。然后检查被代理后的端口信息是否正确:
时间: 2024-10-28 14:04:49