全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1934|回复: 14
打印 上一主题 下一主题

关于多cdn嵌套获取访客真实ip

[复制链接]
跳转到指定楼层
1#
发表于 2023-6-23 23:53:15 来自手机 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 By小酷 于 2023-6-24 01:14 编辑

源站甲骨文德国,国外走cloudflare,国内再套 CloudFront自选。
获取访客真实ip的配置是
real_ip_header CF-CONNECTING-IP; cloudflare的
real_ip_header CloudFront-Viewer-Address; 这个不用说了吧

这两个写一起会报错,只能选择一个,如何才能同时支持2个呢


搞定了
………………………………………………………………………………

首先主配置http下包含以下文件
include cf.conf

里面的内容为

real_ip_recursive on;
    real_ip_header X-Forwarded-For;
# 设置Cloudflare的真实IP头部字段
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 104.16.0.0/12;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2c0f:f248::/32;
    set_real_ip_from 2a06:98c0::/29;
   
    # 设置CloudFront的真实IP头部字段
  set_real_ip_from 108.138.0.0/15;
  set_real_ip_from 108.156.0.0/14;
  set_real_ip_from 116.129.226.0/25;
  set_real_ip_from 116.129.226.128/26;
  set_real_ip_from 118.193.97.128/25;
  set_real_ip_from 118.193.97.64/26;
  set_real_ip_from 119.147.182.0/25;
  set_real_ip_from 119.147.182.128/26;
  set_real_ip_from 120.232.236.0/25;
  set_real_ip_from 120.232.236.128/26;
  set_real_ip_from 120.253.240.192/26;
  set_real_ip_from 120.253.241.160/27;
  set_real_ip_from 120.253.245.128/26;
  set_real_ip_from 120.253.245.192/27;
  set_real_ip_from 120.52.12.64/26;
  set_real_ip_from 120.52.153.192/26;
  set_real_ip_from 120.52.22.96/27;
  set_real_ip_from 120.52.39.128/27;
  set_real_ip_from 13.113.196.64/26;
  set_real_ip_from 13.113.203.0/24;
  set_real_ip_from 13.124.199.0/24;
  set_real_ip_from 13.210.67.128/26;
  set_real_ip_from 13.224.0.0/14;
  set_real_ip_from 13.228.69.0/24;
  set_real_ip_from 13.233.177.192/26;
  set_real_ip_from 13.249.0.0/16;
  set_real_ip_from 13.32.0.0/15;
  set_real_ip_from 13.35.0.0/16;
  set_real_ip_from 13.48.32.0/24;
  set_real_ip_from 13.54.63.128/26;
  set_real_ip_from 13.59.250.0/26;
  set_real_ip_from 130.176.0.0/17;
  set_real_ip_from 130.176.0.0/18;
  set_real_ip_from 130.176.128.0/18;
  set_real_ip_from 130.176.128.0/21;
  set_real_ip_from 130.176.136.0/23;
  set_real_ip_from 130.176.140.0/22;
  set_real_ip_from 130.176.144.0/20;
  set_real_ip_from 130.176.160.0/19;
  set_real_ip_from 130.176.192.0/19;
  set_real_ip_from 130.176.224.0/20;
  set_real_ip_from 130.176.64.0/21;
  set_real_ip_from 130.176.72.0/22;
  set_real_ip_from 130.176.76.0/24;
  set_real_ip_from 130.176.78.0/23;
  set_real_ip_from 130.176.80.0/22;
  set_real_ip_from 130.176.86.0/23;
  set_real_ip_from 130.176.88.0/21;
  set_real_ip_from 130.176.96.0/19;
  set_real_ip_from 143.204.0.0/16;
  set_real_ip_from 144.220.0.0/16;
  set_real_ip_from 15.158.0.0/16;
  set_real_ip_from 15.188.184.0/24;
  set_real_ip_from 15.207.13.128/25;
  set_real_ip_from 15.207.213.128/25;
  set_real_ip_from 18.154.0.0/15;
  set_real_ip_from 18.160.0.0/15;
  set_real_ip_from 18.164.0.0/15;
  set_real_ip_from 18.172.0.0/15;
  set_real_ip_from 18.192.142.0/23;
  set_real_ip_from 18.200.212.0/23;
  set_real_ip_from 18.216.170.128/25;
  set_real_ip_from 18.229.220.192/26;
  set_real_ip_from 18.238.0.0/15;
  set_real_ip_from 18.244.0.0/15;
  set_real_ip_from 18.64.0.0/14;
  set_real_ip_from 180.163.57.0/25;
  set_real_ip_from 180.163.57.128/26;
  set_real_ip_from 204.246.164.0/22;
  set_real_ip_from 204.246.166.0/24;
  set_real_ip_from 204.246.168.0/22;
  set_real_ip_from 204.246.172.0/24;
  set_real_ip_from 204.246.173.0/24;
  set_real_ip_from 204.246.174.0/23;
  set_real_ip_from 204.246.176.0/20;
  set_real_ip_from 205.251.200.0/21;
  set_real_ip_from 205.251.208.0/20;
  set_real_ip_from 205.251.218.0/24;
  set_real_ip_from 205.251.249.0/24;
  set_real_ip_from 205.251.250.0/23;
  set_real_ip_from 205.251.252.0/23;
  set_real_ip_from 205.251.254.0/24;
  set_real_ip_from 216.137.32.0/19;
  set_real_ip_from 223.71.11.0/27;
  set_real_ip_from 223.71.71.128/25;
  set_real_ip_from 223.71.71.96/27;
  set_real_ip_from 3.10.17.128/25;
  set_real_ip_from 3.101.158.0/23;
  set_real_ip_from 3.11.53.0/24;
  set_real_ip_from 3.128.93.0/24;
  set_real_ip_from 3.134.215.0/24;
  set_real_ip_from 3.231.2.0/25;
  set_real_ip_from 3.234.232.224/27;
  set_real_ip_from 3.236.169.192/26;
  set_real_ip_from 3.236.48.0/23;
  set_real_ip_from 3.35.130.128/25;
  set_real_ip_from 34.195.252.0/24;
  set_real_ip_from 34.216.51.0/25;
  set_real_ip_from 34.223.12.224/27;
  set_real_ip_from 34.223.80.192/26;
  set_real_ip_from 34.226.14.0/24;
  set_real_ip_from 35.158.136.0/24;
  set_real_ip_from 35.162.63.192/26;
  set_real_ip_from 35.167.191.128/26;
  set_real_ip_from 36.103.232.0/25;
  set_real_ip_from 36.103.232.128/26;
  set_real_ip_from 44.227.178.0/24;
  set_real_ip_from 44.234.108.128/25;
  set_real_ip_from 44.234.90.252/30;
  set_real_ip_from 52.124.128.0/17;
  set_real_ip_from 52.15.127.128/26;
  set_real_ip_from 52.199.127.192/26;
  set_real_ip_from 52.212.248.0/26;
  set_real_ip_from 52.220.191.0/26;
  set_real_ip_from 52.222.128.0/17;
  set_real_ip_from 52.46.0.0/18;
  set_real_ip_from 52.46.0.0/22;
  set_real_ip_from 52.46.16.0/20;
  set_real_ip_from 52.46.32.0/19;
  set_real_ip_from 52.46.4.0/23;
  set_real_ip_from 52.47.139.0/24;
  set_real_ip_from 52.52.191.128/26;
  set_real_ip_from 52.56.127.0/25;
  set_real_ip_from 52.57.254.0/24;
  set_real_ip_from 52.66.194.128/26;
  set_real_ip_from 52.78.247.128/26;
  set_real_ip_from 52.82.128.0/19;
  set_real_ip_from 52.82.128.0/23;
  set_real_ip_from 52.82.134.0/23;
  set_real_ip_from 52.84.0.0/15;
  set_real_ip_from 54.182.0.0/16;
  set_real_ip_from 54.182.128.0/20;
  set_real_ip_from 54.182.144.0/21;
  set_real_ip_from 54.182.154.0/23;
  set_real_ip_from 54.182.156.0/22;
  set_real_ip_from 54.182.160.0/21;
  set_real_ip_from 54.182.172.0/22;
  set_real_ip_from 54.182.176.0/21;
  set_real_ip_from 54.182.184.0/22;
  set_real_ip_from 54.182.188.0/23;
  set_real_ip_from 54.182.224.0/21;
  set_real_ip_from 54.182.240.0/21;
  set_real_ip_from 54.182.248.0/22;
  set_real_ip_from 54.192.0.0/16;
  set_real_ip_from 54.230.0.0/17;
  set_real_ip_from 54.230.128.0/18;
  set_real_ip_from 54.230.200.0/21;
  set_real_ip_from 54.230.208.0/20;
  set_real_ip_from 54.230.224.0/19;
  set_real_ip_from 54.233.255.128/26;
  set_real_ip_from 54.239.128.0/18;
  set_real_ip_from 54.239.134.0/23;
  set_real_ip_from 54.239.170.0/23;
  set_real_ip_from 54.239.192.0/19;
  set_real_ip_from 54.239.204.0/22;
  set_real_ip_from 54.239.208.0/21;
  set_real_ip_from 54.240.128.0/18;
  set_real_ip_from 58.254.138.0/25;
  set_real_ip_from 58.254.138.128/26;
  set_real_ip_from 64.252.128.0/18;
  set_real_ip_from 64.252.64.0/18;
  set_real_ip_from 65.8.0.0/16;
  set_real_ip_from 65.9.0.0/17;
  set_real_ip_from 65.9.128.0/18;
  set_real_ip_from 70.132.0.0/18;
  set_real_ip_from 71.152.0.0/17;
  set_real_ip_from 99.79.169.0/24;
  set_real_ip_from 99.84.0.0/16;
  set_real_ip_from 99.86.0.0/16;
  set_real_ip_from 2400:7fc0:500::/40;
  set_real_ip_from 2404:c2c0:500::/40;
  set_real_ip_from 2600:9000:1000::/36;
  set_real_ip_from 2600:9000:2000::/36;
  set_real_ip_from 2600:9000:3000::/36;
  set_real_ip_from 2600:9000:4000::/36;
  set_real_ip_from 2600:9000:5308::/45;
  set_real_ip_from 2600:9000:5310::/44;
  set_real_ip_from 2600:9000:5320::/43;
  set_real_ip_from 2600:9000:5340::/42;
  set_real_ip_from 2600:9000:5380::/41;
  set_real_ip_from 2600:9000:ddd::/48;
  set_real_ip_from 2600:9000:eee::/48;
  set_real_ip_from 2600:9000:f000::/38;
  set_real_ip_from 2600:9000:f400::/40;
  set_real_ip_from 2600:9000:f500::/43;
  set_real_ip_from 2600:9000:f520::/44;
  set_real_ip_from 2600:9000:f534::/46;
  set_real_ip_from 2600:9000:f538::/45;
  set_real_ip_from 2600:9000:f540::/42;
  set_real_ip_from 2600:9000:f580::/41;
  set_real_ip_from 2600:9000:f600::/39;
  set_real_ip_from 2600:9000:f800::/37;
  set_real_ip_from 2600:9000:fff::/48;
2#
发表于 2023-6-23 23:55:02 | 只看该作者
要同时支持两个真实IP头部字段,您可以使用Nginx的real_ip_header和set_real_ip_from指令来配置。以下是一个示例配置:

  1. http {
  2.     # 设置Cloudflare的真实IP头部字段
  3.     real_ip_header CF-CONNECTING-IP;
  4.     set_real_ip_from 103.21.244.0/22;
  5.     set_real_ip_from 103.22.200.0/22;
  6.     set_real_ip_from 103.31.4.0/22;
  7.     set_real_ip_from 104.16.0.0/12;
  8.     set_real_ip_from 108.162.192.0/18;
  9.     set_real_ip_from 131.0.72.0/22;
  10.     set_real_ip_from 141.101.64.0/18;
  11.     set_real_ip_from 162.158.0.0/15;
  12.     set_real_ip_from 172.64.0.0/13;
  13.     set_real_ip_from 173.245.48.0/20;
  14.     set_real_ip_from 188.114.96.0/20;
  15.     set_real_ip_from 190.93.240.0/20;
  16.     set_real_ip_from 197.234.240.0/22;
  17.     set_real_ip_from 198.41.128.0/17;
  18.     set_real_ip_from 199.27.128.0/21;
  19.     set_real_ip_from 2400:cb00::/32;
  20.     set_real_ip_from 2606:4700::/32;
  21.     set_real_ip_from 2803:f800::/32;
  22.     set_real_ip_from 2405:b500::/32;
  23.     set_real_ip_from 2405:8100::/32;
  24.     set_real_ip_from 2c0f:f248::/32;
  25.     set_real_ip_from 2a06:98c0::/29;

  26.     # 设置CloudFront的真实IP头部字段
  27.     real_ip_header CloudFront-Viewer-Address;
  28.     set_real_ip_from 0.0.0.0/0;

  29.     # 其他Nginx配置...
  30. }
复制代码



在上面的示例中,我们使用set_real_ip_from指令指定了Cloudflare和CloudFront的IP地址范围。您可以根据需要添加或修改这些IP地址范围。

请确保在Nginx配置中的正确位置添加这些指令,并重新加载Nginx配置以使其生效。这样,Nginx将同时支持两个真实IP头部字段,并将访客的真实IP地址传递给后端服务器。
3#
 楼主| 发表于 2023-6-24 00:08:58 来自手机 | 只看该作者
兔子淘 发表于 2023-6-23 23:55
要同时支持两个真实IP头部字段,您可以使用Nginx的real_ip_header和set_real_ip_from指令来配置。以下是一 ...

按照你的建议,会提示
directive is duplicate 这个错误
4#
发表于 2023-6-24 00:21:34 | 只看该作者
real_ip_header X-Forwarded-For;
5#
 楼主| 发表于 2023-6-24 00:23:47 来自手机 | 只看该作者
elgt 发表于 2023-6-24 00:21
real_ip_header X-Forwarded-For;

real_ip_header X-Forwarded-For的信息是不准确的,并且可能是伪造的,真实的ip应该为
real_ip_header CF-CONNECTING-IP;跟
real_ip_header CloudFront-Viewer-Address;
6#
发表于 2023-6-24 00:24:01 | 只看该作者
  1. set_real_ip_from 0.0.0.0/0;
  2. real_ip_header X-Forwarded-For;
复制代码
7#
 楼主| 发表于 2023-6-24 00:26:52 来自手机 | 只看该作者
elgt 发表于 2023-6-24 00:24

。。。。。。。你好像并没有看我问什么
8#
发表于 2023-6-24 00:27:03 | 只看该作者
By小酷 发表于 2023-6-24 00:23
real_ip_header X-Forwarded-For的信息是不准确的,并且可能是伪造的,真实的ip应该为
real_ip_header CF ...

你只需要设置为只允许Cloudfare和Cloudfront访问即可,这样就不会有伪造的问题
9#
发表于 2023-6-24 00:28:02 | 只看该作者
你在CF的头部控制上面,传一个CloudFront-Viewer-Address给源站不就得了
10#
发表于 2023-6-24 00:29:51 | 只看该作者
nginx同一个配置下不能有两个重复的real_ip_header
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-27 22:24 , Processed in 0.133617 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表