接入上海云盾CDN后获取访客真实IP

云盾提供的CDN节点列表:http://v4.yundun.com/help/219.html
我们把IP全复制下来,写入文件1,此时文件1是如下样子:
云盾CDN节点

然而Nginx能识别的是这个样子:

set_real_ip_from 103.245.81.0/24;
set_real_ip_from 61.160.245.0/24;
set_real_ip_from 120.195.110.0/24;
set_real_ip_from 112.82.223.0/24;
set_real_ip_from 60.191.139.0/24;
set_real_ip_from 101.64.234.0/24;

我们执行

grep . 1

去掉空行:
云盾CDN节点
执行

grep . 1 |awk '{print $1}'

去掉尾巴,只要第一段:
云盾CDN节点
最后我们用sed把文本变成我们想要的样子:

grep . 1 |awk '{print $1}' |sed 's/^/set_real_ip_from /;s/$/;/'

云盾CDN节点
把下面两句加在IP列表的下面

real_ip_header X-Real-ip;
real_ip_recursive on;

最终的样子:

set_real_ip_from 59.56.19.0/24;
set_real_ip_from 59.56.79.0/24;
set_real_ip_from 59.56.78.0/24;
set_real_ip_from 115.231.25.0/24;
set_real_ip_from 183.131.214.0/24;
set_real_ip_from 103.36.210.0/24;
set_real_ip_from 111.1.37.0/24;
set_real_ip_from 122.9.179.0/24;
set_real_ip_from 122.9.244.0/24;
set_real_ip_from 123.60.74.0/24;
set_real_ip_from 123.60.150.0/24;
set_real_ip_from 123.60.175.0/24;
set_real_ip_from 123.60.222.0/24;
set_real_ip_from 123.60.248.0/24;
set_real_ip_from 38.134.121.0/24;
set_real_ip_from 204.128.60.0/24;
set_real_ip_from 112.175.238.0/24;
set_real_ip_from 198.44.242.0/24;
real_ip_header X-Real-ip;
real_ip_recursive on;

最后,log_format引用的时候用"$HTTP_X_REAL_IP" 引用。
更多细节,参见https://hqidi.com/75.html

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

本文链接地址: 接入上海云盾CDN后获取访客真实IP

8 条评论

  • 明月清风 2017年12月16日 回复

    这是一个长知识的教程

  • 姜辰 2017年12月16日 回复

    云盾当初把我的空间给抓取炸了。- -

  • he 2017年12月17日 回复

    云盾的CDN节点好像有点少啊。

  • 懿古今 2017年12月24日 回复

    这个真是IP我好像从未关注过

  • 西枫里博客 2018年1月3日 回复

    能获取长宽、歌华、铁通、有线通的IP么?

    • dige 2018年1月7日 回复 作者

      目前应该没有技术能获取到吧?就好像你要获取到公司网关后面,员工的内网IP一样。

  • 迎風别葉index 2018年1月18日 回复

    说到CDN,360的免费CDN给的节点不全,前不久502了 。。

  • siyochen 2018年1月26日 回复

    上次被攻击了。唉

发表评论

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

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