nginx获取用户真实ip

我们访问互联网上的服务时,大多数时,客户端并不是直接访问到服务端的,而是客户端首先请求到反向代理,反向代理再转发到服务端实现服务访问,通过反向代理实现路由/负载均衡等策略。这样在服务端拿到的客户端IP将是反向代理IP,而不是真实客户端IP,因此需要想办法来获取到真实客户端IP

客户端访问服务端的数据流走向
Client(172.25.0.1) --> ADSL( 192.168.0.1) --> cdn(10.0.0.1) --> SLB(反向代理)11.0.0.1 --> server(nginx)12.0.0.1
可以看出,服务端根本获取不到真实的客户端ip,只能获取到上一层服务的ip,那么nginx怎样才能获取到真实的ip呢?
用一台服务器模拟实现获取本机ip
源码编译,添加一个模块,获取real ip
[root@base1 ~]# tar zxf nginx-1.14.2.tar.gz
[root@base1 ~]# yum install -y gcc pcre-devel zlib-devel # 这是编译nginx的依赖包
[root@base1 ~]# cd nginx-1.14.2
[root@base1 nginx-1.14.2]# make
[root@base1 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_realip_module # 添加了一个获取realip的模块
[root@base1 nginx-1.14.2]# make && make install

[root@base1 nginx-1.14.2]# nginx -V # 查看编译参数
在这里插入图片描述
[root@base1 nginx-1.14.2]# cd /usr/local/nginx/conf/
[root@base1 conf]# vim nginx.conf

添加虚拟主机

 server {
     listen 80;
     server_name base1.westos.org;
     location / {
         return 200;
     }
 }

在这里插入图片描述
[root@base1 conf]# nginx # 开启nginx服务
[root@base1 conf]# nginx -s reload
[root@base1 conf]# vim /etc/hosts # 添加域名
172.25.78.11 base1 base1.westos.org
测试
[root@base1 conf]# curl -I base1.westos.org
在这里插入图片描述
当检测

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值