nginx之配置proxy_set_header问题梳理
客户端请求web服务,客户端:
ip:192.168.223.1
nginx作为反向代理服务器:
192.168.223.136 nginx作为后端web服务器:
192.168.223.137
前提条件:
配置nginx转发到后端服务器。
server {
listen ;
server_name 192.168.223.136;
location / {
root "/www/html";
index index.html;
#auth_basic "required auth";
#auth_basic_user_file "/usr/local/nginx/users/.htpasswd";
error_page /.html;
}
location /images/ {
root "/www";
rewrite ^/images/bbs/(.*\.jpeg)$ /images/$ break;
rewrite ^/images/www/(.*)$ http://192.168.223.136/$1 redirect;
}
location /basic_status {
stub_status;
}
location ^~/proxy_path/ {
root "/www/html";
index index.html;
proxy_pass http://192.168.223.137/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
location ^~/proxy_path/ {
root "/www/html";
index index.html;
proxy_pass http://192.168.223.137/;
将左侧匹配到的/proxy_path/开头的url全部转发到后端服务器 192.168.223.137。
现在一一测试各个proxy_set_header设置的变量的内容:
1、proxy_set_header Host $host;
将136代理服务器,137后端服务器的log_format修改为如下:
然后开启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代理服务器,也就是客户端请求的host。
2、proxy_set_header Host $proxy_host;
将设置修改为上述 proxy_host 然后重启ngxin代理服务器136
[root@wadeson nginx]# sbin/nginx -s reload
重新请求代理页面:http://192.168.223.136:8080/proxy_path/index.html,然后日志如下:
首先查看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",添加了这个变量的值
重新请求需要访问的地址http://192.168.223.136:8080/proxy_path/index.html
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"';
重新请求需要访问的地址http://192.168.223.136:8080/proxy_path/index.html
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
但是区别在于:
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.13 ...
- nginx缓存配置的操作记录梳理
web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...
- spring4+websocket+nginx详细配置
实现的版本jdk1.7.0_25, tomcat7.0.47.0, Tengine/2.1.1 (nginx/1.6.2), servlet3.0, spring4.2.2 使用maven导入版本3. ...
- 理解nginx的配置
Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...
- 《深入理解Nginx》阅读与实践(一):Nginx安装配置与HelloWorld
最近在读陶辉的<深入理解Nginx:模块开发与架构解析>,一是想跟着大牛练练阅读和编写开源代码的能力,二是想学学Nginx优秀的架构设计,三是想找一个点深入下Linux下网络编程的细节.侯 ...
- nginx的配置总结
总体而言,nginx的配置比起apache来是要简洁很多,而言容易理解得多的,另外官网的文档也十分的简洁易懂.我们先看一个简化版的配置文件nginx.conf: #user nobody; worke ...
- 图文解说:Nginx+tomcat配置集群负载均衡
图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用 作者:niumd Blog:http://ari.iteye ...
- nginx服务配置---php服务接入
前言: 最近要搭建一个内部的wiki系统, 网上搜了一圈, 也从知乎上搜集了一些大神的评价和推荐. 重点找了几个开源的wiki系统, 不过发现他们都是采用php来实现的. 于是乎需要配置php环境, ...
- nginx性能配置参数说明:
nginx的配置:main配置段说明一.正常运行的必备配置: 1.user username [groupname]; 指定运行worker进程的用户和组 2.pid /path/to/pidfile ...
随机推荐
- Linux文件系统属性权限chattr与lsattr命令
有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux ...
- Freestream边界条件【翻译】
翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/93093-freestream-boundary-cond ...
- IdentityServer4入门四:应用Implicit模式保护网站(上)
我们先新增一个网站,名为“ClientMvc",也是asp.net core Web应用程序(模型视图控制器) 使用nuget安装以下引用 Microsoft.AspNetCore.Auth ...
- spring boot 之 如何创建spring boot项目
创建spring boot的方式有非常多,今天我们使用maven来进行创建spring boot项目,因为maven使用的非常广泛,也很好用,很多IDE也都支持maven. 1 创建maven项目 1 ...
- 2018-2019-2 《网络对抗技术》Exp8 Web基础 Week11-12 20165233
Exp8 Web基础 目录 一.基础问题 二.实验步骤 实验点一:Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. 实验 ...
- 微信小程序:自定义导航栏
在小程序开发的时候会遇到一些页面不需要头部导航,或像淘宝的商品详情一样自定义的导航栏.那先要清楚这导航可不能写死,每种手机的导航都各不相同. 一.在app.json的window对象中定义导航的样式: ...
- windows系统下Jenkins 持续集成安装使用
先要下载安装Tomcat,基于Java的web项目可以通过Tomcat运行.下载Jenkins,注意要安装在Tomcat的webapps目录下.安装完Jenkins会自动通过浏览器打开http://l ...
- .htaccess文件常见到[QSA][NC][L][R][F]的意思
.htaccess文件常见到[QSA][NC][L][R][F]几个字符在代码末尾,下边列出它们在静态规则中的作用,以备参考. NC : no case,不区分大小写,忽略大小写: L : l ...
- SpringBoot持久层技术
一.Springboot整合mybatis maven中添加对数据库与mybatis的依赖 <dependencies> <dependency> <groupId> ...
- Mac使用brew安装nginx,并解决端口80访问权限问题
1.安装 brew install nginx 2.修改配置文件 sudo vi /usr/local/etc/nginx/nginx.conf 修改默认的8080端口为80 修改日志文件地方 err ...