Nginx使用realip_module获取代理后的真实客户ip地址

测试架构图

在这里插入图片描述

目标:配置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 ,实验搭建完成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值