nginx之配置proxy_set_header
nginx之配置proxy_set_header
win10客户端请求web服务,win10的ip:192.168.223.1
nginx作为反向代理服务器:192.168.223.136
nginx作为后端web服务器:192.168.223.137
前提条件:配置nginx转发到后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
将左侧匹配到的/proxy_path/开头的url全部转发到后端服务器192.168.223.137
现在⼀⼀测试各个proxy_set_header设置的变量的内容
1、proxy_set_header Host $host;
将136代理服务器,137后端服务器的log_format修改为如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
proxy_set_header Host $host;这⾥的Host变量的值对应的就是⽇志中的$http_host 的值
当win10⽤户访问时
查看代理服务器和后端服务器的地址,可以发现$http_host对应的值为192.168.223.136:8080
192.168.223.1 - - [18/Jul/2017:10:21:25 +0800] "GET /favicon.ico HTTP/1.1" 192.168.223.136:8080 404 24
"192.168.223.136:8080/proxy_path/index.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"
由于我将后端服务器关闭了,所以出现502⽹管错误:
然后开启137后端nginx,查看⽇志:
192.168.223.136 "192.168.223.1" - - [17/Jul/2017:17:06:44 +0800] "GET /index.html HTTP/1.0" "192.168.223.136" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "192.168.223.1"
即验证了proxy_set_header Host $host;  $host就是nginx代理服务器,也就是win10客户端请求的host
2、proxy_set_header Host $proxy_host;
将设置修改为上述proxy_host然后重启ngxin代理服务器136
[root@wadeson nginx]# sbin/nginx -s reload
⾸先查看136代理服务器的⽇志:
192.168.223.1 - - [18/Jul/2017:10:30:12 +0800] "GET /proxy_path/index.html HTTP/1.1" 192.168.223.136:8080 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"
因为win10是136的客户端,请求的host为192.168.223.136:8080,⽽nginx代理服务器作为137后端服务器的客户端,将请求的报⽂⾸部重新封装,将proxy_host封装为请求的host
那么137上⾯⽇志请求的host就是其⾃⾝,proxy_host就是代理服务器请求的host也就是后端服务器137
192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:30:12 +0800] "GET /index.html HTTP/1.0" "192.168.223.137" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"
3、proxy_set_header Host $host:$proxy_port;
怎样设置代理服务器
了解了上⾯的知识,那么此处对应的host就知道代表的啥了,$host代表转发服务器,$proxy_port代表136转发服务器请求后端服务器的端⼝,也就是80
于是观察136、137的⽇志进⾏验证:
192.168.223.1 - - [18/Jul/2017:10:38:38 +0800] "GET /proxy_path/index.html HTTP/1.1" 192.168.223.136:8080 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"
192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:38:38 +0800] "GET /index.html HTTP/1.0" "192.168.223.136:80" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"
4、proxy_set_header X-Real-IP $remote_addr;
将$remote_addr的值放进变量X-Real-IP中,此变量名可变,$remote_addr的值为客户端的ip
nginx转发136服务器⽇志格式为:
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
nginx后端137服务器的⽇志格式:
log_format main '$remote_addr "$http_x_real_ip" - $remote_user [$time_local] "$request" "$http_host" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
两者区别在于"$http_x_real_ip",添加了这个变量的值
136的⽇志:
192.168.223.1 - - [18/Jul/2017:10:45:07 +0800] "GET /proxy_path/index.html HTTP/1.1" 192.168.223.136:8080 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"
137的⽇志:
192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:45:07 +0800] "GET /index.html HTTP/1.0" "192.168.223.136:80" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"
红⾊标记的就是"$http_x_real_ip"的值,即可以看见⽤户真实的ip,也就是客户端的真实ip
5、proxy_set_header X-Forwarded-For $remote_addr;
理解了上⾯的含义那么这个封装报⽂的意思也就请求了
⾸先还是⽐对136和137的⽇志格式:
136代理服务器的⽇志格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
137后端服务器的⽇志格式:
log_format main '$remote_addr "$http_x_real_ip" - $remote_user [$time_local] "$request" "$http_host" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
136的⽇志显⽰:
192.168.223.1 - - [18/Jul/2017:10:51:25 +0800] "GET /proxy_path/index.html HTTP/1.1" 192.168.223.136:8080 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "-",最后⼀个字段"$http_x_forwarded_for"对应的为空值
137的⽇志显⽰:
192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:51:25 +0800] "GET /index.html HTTP/1.0" "192.168.223.136:80" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"
可以看出137后端服务器成功的显⽰了真实客户端的ip
6、proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5、6两者的区别:
在只有⼀个代理服务器的转发的情况下,两者的效果貌似差不多,都可以真实的显⽰出客户端原始ip
但是区别在于:
$proxy_add_x_forwarded_for变量包含客户端请求头中的"X-Forwarded-For",与$remote_addr两部分,他们之间⽤逗号分开。
举个例⼦,有⼀个web应⽤,在它之前通过了两个nginx转发,www.linuxidc 即⽤户访问该web通过两台nginx。
在第⼀台nginx中,使⽤
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
现在的$proxy_add_x_forwarded_for变量的"X-Forwarded-For"部分是空的,所以只有$remote_addr,⽽$remote_addr的值是⽤户的ip,于是赋值以后,X-Forwarded-For变量的值就是⽤户的真实的ip地址了。
到了第⼆台nginx,使⽤
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
现在的$proxy_add_x_forwarded_for变量,X-Forwarded-For部分包含的是⽤户的真实ip,$remote_addr部分的值是上⼀台nginx的ip地址,于是通过这个赋值以后现在的X-Forwarded-For的值就变成了“⽤户的真实ip,第⼀台nginx的ip”,这样就清楚了吧。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。