1)如下测试,不设置 proxy_set_header
Nginx 配置:
upstream
test
{
server 192.168.1.123:9099;
server 192.168.1.123:58080;
}
server {
listen 5800;
server_name 192.168.1.123;
root
/usr/share/nginx/html
;
include
/etc/nginx/default
.d/*.conf;
location / {
proxy_pass http:
//test
;
}
测试jsp 想获取客户端IP、客户端port、代理服务器IP、代理服务器port
<%@page contentType=
"text/html; charset=UTF-8"
trimDirectiveWhitespaces=
"true"
%>
<%
String scheme = request.getScheme();
String serverName = request.getServerName();
String remoteName = request.getRemoteAddr();
String realIP = request.getHeader(
"X-Forwarded-For"
);
String realIP2 = request.getHeader(
"X-Real-IP"
);
String Host = request.getHeader(
"Host"
);
int port = request.getServerPort();
int portR = request.getRemotePort();
String requestURIC1 = scheme+
"://"
+realIP+
":"
+portR;
String requestURIC2 = scheme+
"://"
+realIP2+
":"
+portR;
String requestURIC3 = scheme+
"://"
+remoteName+
":"
+portR;
String requestURI = scheme+
"://"
+serverName+
":"
+port;
%>
其中:
客户端地址1:<%=requestURIC1 %>
客户端地址2:<%=requestURIC2 %>
客户端地址3:<%=requestURIC3%>
服务器地址1:<%=requestURI%>
服务器地址2:<%=Host%>
测试结果
客户端地址1:http:
//null
:58828
客户端地址2:http:
//null
:58828
客户端地址3:http:
//192
.168.1.123:58828
服务器地址1:http:
//test
:80
服务器地址2:
test
Nginx日志
192.168.1.177 -20508---5800 [25
/Aug/2016
:16:34:13 +0800]
"GET /docs/test.jsp HTTP/1.1"
200 223 "
其中客户端IP不能获取到,而通过request.getRemoteAddr();
获取的IP是代理服务器IP,而不是客户端IP,而在nginx中$remote_addr变量的值是客户端的IP,可见remoteaddr没有传递。
而server_port值也不对,当前值为5800,当前打印出的是80。
而当前代理为http:
//test
所有通过host得到的是
test
。
客户端port也获取不到值为20508,可传给应用的是58828
----------------------------------------------------------------------------------------------------------------
2)如下测试,设置 proxy_set_header
Nginx 配置:
upstream
test
{
server 192.168.1.123:9099;
server 192.168.1.123:58080;
}
server {
listen 5800;
server_name 192.168.1.123;
root
/usr/share/nginx/html
;
include
/etc/nginx/default
.d/*.conf;
location / {
proxy_pass http:
//test
;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
测试页面改成:
<%@page contentType=
"text/html; charset=UTF-8"
trimDirectiveWhitespaces=
"true"
%>
<%
String scheme = request.getScheme();
String serverName = request.getServerName();
String remoteName = request.getRemoteAddr();
String realIP = request.getHeader(
"X-Forwarded-For"
);
String realIP2 = request.getHeader(
"X-Real-IP"
);
String Host = request.getHeader(
"Host"
);
int port = request.getServerPort();
int portR = request.getRemotePort();
String portR2 = request.getHeader(
"X-Real-Port"
);
String requestURIC1 = scheme+
"://"
+realIP+
":"
+portR;
String requestURIC2 = scheme+
"://"
+realIP2+
":"
+portR;
String requestURIC3 = scheme+
"://"
+remoteName+
":"
+portR;
String requestURI = scheme+
"://"
+serverName+
":"
+port;
%>
其中:
客户端地址1:<%=requestURIC1 %>
客户端地址2:<%=requestURIC2 %>
客户端地址3:<%=requestURIC3%>
服务器地址1:<%=requestURI%>
服务器地址2:<%=Host%>
客户端port2:<%=portR2%>
客户端地址1:http:
//192
.168.1.177:21548
客户端地址2:http:
//192
.168.1.177:21548
客户端地址3:http:
//192
.168.1.123:21548
服务器地址1:http:
//192
.168.1.123:5800
服务器地址2:192.168.1.123:5800
客户端port2:20604
nginx日志:
192.168.1.177 -20604---5800 [25
/Aug/2016
:16:38:42 +0800]
"GET /docs/test.jsp HTTP/1.1"
200 275
"-"
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
"-"
除request.getRemoteAddr();获取的值不对外,其他值都是对的。
getRemoteAddr获取的是代理的请求地址。
因重定义了host,所以
test
值被改写成代理服务器IP。
因重定义了 X-Real-PORT-并传递$remote_port,客户端port也获取正确了。