CDN后Nginx获取用户真实IP地址

网站接入网站卫士后,$remote_addr变量获取不到访客的真实IP,拿到的都是360CDN节点的IP,这样,nginx的日志就失去了意义,360那边说“用户的真实ip被网站卫士放在x-forward-for里面”,然而我们直接用下面的日志格式,依然是获取不到访客真实IP的:

log_format '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
	              '$status $body_bytes_sent "$http_referer" '
	              '"$http_user_agent"';

我们需要用到一个nginx官方的HTTP Realip模块,本模块默认是不会被编译进nginx的。
1、找到安装nginx的源码根目录,然而我的机器已经在线473天了,

压根就找不到源码了,只能重新下载,下载nginx源码,教大家一个小技巧,nginx官方对源码资源的管理做得很好,比如你要下载1.4.4的版本:
wget http://nginx.org/download/nginx-1.4.4.tar.gz
你要下载哪个版本只要修改后面的版本号就行。
tar xvzf nginx-1.4.4.tar.gz

2、查看ngixn版本极其编译参数

/usr/local/nginx/sbin/nginx -V

3、进入nginx源码目录

cd nginx-1.4.4

4、以下是重新编译的代码和模块

./configure --user=www --group=www --prefix=/usr/local/nginx --without-http-cache --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module

make 千万别make install(敲黑板),否则就覆盖安装了

make完之后在objs目录下就多了个nginx,这个就是新版本的程序了

5、备份旧的nginx程序

cp /usr/local/nginx/sbin/nginx{,.bak}

把新的nginx程序覆盖旧的

cp objs/nginx /usr/local/nginx/sbin/nginx

6、测试新的nginx程序是否正确

/usr/local/nginx/sbin/nginx -t

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/nginx/conf/nginx.conf test issuccessful

7、温柔重启nginx

/usr/local/nginx/sbin/nginx -s reload

8、查看ngixn版本极其编译参数

/usr/local/nginx/sbin/nginx -V

至此realip模块添加完毕。

我们把下面的配置写入nginx.conf的http段,就能拿到访客的真实IP。

set_real_ip_from   183.136.133.0/24;
set_real_ip_from   202.102.85.0/24;
set_real_ip_from   61.160.224.0/24;
set_real_ip_from   182.140.227.0/24;
set_real_ip_from   221.204.14.0/24;
set_real_ip_from   222.73.144.0/24;
set_real_ip_from   61.240.144.0/24;
set_real_ip_from   113.17.174.0/24;
set_real_ip_from   125.88.189.0/24;
set_real_ip_from   120.52.18.0/24;
set_real_ip_from   119.84.15.0/24;
set_real_ip_from   180.163.224.0/24;
real_ip_header    X-Forwarded-For;
real_ip_recursive on;

set_real_ip_from: 真实服务器上一级代理的IP地址或者IP段,可以写多行
real_ip_header: 从哪个header头检索出要的IP地址
real_ip_recursive:递归排除IP地址,ip串从右到左开始排除set_real_ip_from里面出现的IP,如果出现了未出现这些ip段的IP,那么这个IP将被认为是用户的IP。
假设一台nginx位于Squid的后端,Squid设置了X-Forwarded-For头信息,记录了用户的真实IP,但是,如果存在多级代理,X-Forwarded-For头信息的内容值将变成以逗号分隔的多个IP地址。
X-Forwarded-For: 59.63.247.177, 180.136.250.182, 192.168.6.1

原创文章,转载请注明: 转载自笛声

本文链接地址: CDN后Nginx获取用户真实IP地址

6 条评论

  • 马化腾 2017年4月26日 回复

    看来阿里云还是挺稳定的。

  • 张戈 2017年8月26日 回复

    还可以尝试下这个:
    在nginx的http模块内加入如下代码,就可以取得真实IP变量:$clientRealIP,可以丢到日志format里。

    #获取用户真实IP,并赋值给变量$clientRealIP
    map $http_x_forwarded_for $clientRealIp {
    "" $remote_addr;
    ~^(?P[0-9\.]+),?.*$ $firstAddr;
    }

    • dige 2017年8月26日 回复 作者

      这样的话,是不是就不用把CDN节点的IP列表写入nginx的配置文件了??

      • 张戈 2017年8月26日 回复

        是的,你可以试试,我一直在用

        • dige 2017年9月10日 回复 作者

          难过,试了没用,拿到的IP还是CDN节点的IP,log_format里面获取访客ip的变量是$clientRealIP ? real_ip_header相关的参数要配置吗?

    • foo 2019年1月17日 回复

      推荐一个更 NB 的方法,`set_real_ip_from 0.0.0.0/0`,直接取到最左侧的 IP。

      https://rj03hou.github.io/aws,k8s,ingress/ingress%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E7%9C%9F%E5%AE%9Eip/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Copyright © 2015-2024 笛声博客 All Rights Reserved     浙ICP备15036123号-1