测试架构图
目标:配置nginx服务,使其客户10.1地址被最后的10.100所解析
步骤
1, 三台虚拟机 分别安装nginx
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
> yum install -y nginx
编写配置文件,开启realip_module,并且开启请求头携带参数,使其最后一层代理能够解析
修改102的配置
>vi /etc/nginx/conf.d/default.conf
location /test {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.10.101:80;
#或者
proxy_pass http://Website; 这样写也行 麻烦点去定义一个upstream
}
修改101的配置
>vi /etc/nginx/conf.d/default.conf
location /test {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.10.100:80;
}
修改101的配置
> vi /etc/nginx/nginx.conf
#过滤掉本机信任的ip也就是我们的代理机
set_real_ip_from 192.168.10.101;
set_real_ip_from 192.168.10.102;
#从哪个header头检索出要的IP地址。
real_ip_header X-Forwarded-For;
#递归的去除所配置中的可信IP。
real_ip_recursive on;
测试
打开电脑本地浏览器访问
http://192.168.10.102/test
查看100服务器打印的nginx日志
tail -f /var/log/nginx/host.access.log
可以看见 客户机的ip地址不再是101或者102本地的代理机 而是我们本地的真实IP ,实验搭建完成!