跳到主內容

Nginx 與 CloudFlare 修改CDN 位置

BUBU 目前有在使用 CloudFlare 服務,那由於從那裡來的 IP 都是 CDN 的 IP ,所以無法查詢到是使用者是從那裡來的,因此有時候 CloudFlare 有時候如果修改 CDN 上的 IP 的話,會無法正確反解來源的 IP

獲取 CDN 上的 IP

在開始之前先取得 CDN 上節點 IP,這個 CDN 服務商都會提供這樣子的 IP
例如:CloudFlare 節點位置:https://www.cloudflare.com/ips/

#CloudFlare IPv4
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
104.16.0.0/12
108.162.192.0/18
131.0.72.0/22
141.101.64.0/18
162.158.0.0/15
172.64.0.0/13
173.245.48.0/20
188.114.96.0/20
190.93.240.0/20
197.234.240.0/22
198.41.128.0/17
#CloudFlare IPv6
2400:cb00::/32
2405:8100::/32
2405:b500::/32
2606:4700::/32
2803:f800::/32
2c0f:f248::/32
2a06:98c0::/29

Nginx 設定檔

編輯 Nginx 主要設定檔 nginx.conf 增加下面的位置,裡面的註解不要刪除,後面自動更新列表會使用到

#cfip4
set_real_ip_from 103.21.244.0/22; #cfipv4
set_real_ip_from 103.22.200.0/22; #cfipv4
set_real_ip_from 103.31.4.0/22; #cfipv4
set_real_ip_from 104.16.0.0/12; #cfipv4
set_real_ip_from 108.162.192.0/18; #cfipv4
set_real_ip_from 131.0.72.0/22; #cfipv4
set_real_ip_from 141.101.64.0/18; #cfipv4
set_real_ip_from 162.158.0.0/15; #cfipv4
set_real_ip_from 172.64.0.0/13; #cfipv4
set_real_ip_from 173.245.48.0/20; #cfipv4
set_real_ip_from 188.114.96.0/20; #cfipv4
set_real_ip_from 190.93.240.0/20; #cfipv4
set_real_ip_from 197.234.240.0/22; #cfipv4
set_real_ip_from 198.41.128.0/17; #cfipv4
#cfip6
set_real_ip_from 2400:cb00::/32; #cfipv6
set_real_ip_from 2405:8100::/32; #cfipv6
set_real_ip_from 2405:b500::/32; #cfipv6
set_real_ip_from 2606:4700::/32; #cfipv6
set_real_ip_from 2803:f800::/32; #cfipv6
set_real_ip_from 2c0f:f248::/32; #cfipv6
set_real_ip_from 2a06:98c0::/29; #cfipv6
#lock
#使用以下任意一個即可
#eal_ip_header CF-Connecting-IP; 
real_ip_header X-Forwarded-For;

這裡的 CF-Connecting-IP (CloudFlare額外提供) 是根據 CDN 服務商提供的返回IP地址的請求而決定的,正常選擇第二段即可,具體需要看 CDN 服務商的文檔。

關於 CF-Connecting-IP 在這裡有說明 https://support.cloudflare.com/hc/en-us/articles/200170986

F-Connecting-IP

To provide the client (visitor) IP address for every request to the origin, Cloudflare adds the CF-Connecting-IP header.
"CF-Connecting-IP: A.B.C.D"
Where A.B.C.D is the client's IP address, also known as the original visitor IP address.

經過這樣的簡單配置,後端 NGINX 就可以正常取得 IP 位址了,但是 CDN 服務商有時會增加或者更換節點的 IP 都是會更動,以下方法會教怎麼去修改。

自動取得 CDN 節點列表

建立一個腳本來取得 CDN 列表,然後再更新到 NGINX 服務上

#/bin/sbin

#變量設定
#Nginx配置文件
nginx=/etc/nginx/nginx.conf
#臨時文件夾
mude=/root/cfip
#日誌文件
log=/root/cfip/log/cfip.log

#初始化
oneup4=0
oneup6=0
updata=0
if [ ! -d "$mude" ]; then
  sudo mkdir $mude
fi

#取得最新IPV4、IPV6地址
sudo wget -q https://www.cloudflare.com/ips-v4 -O $mude/ipv4.cf > $log
sudo wget -q https://www.cloudflare.com/ips-v6 -O $mude/ipv6.cf > $log

#判斷是否第一次執行腳本
if [ ! -e "$mude/ipv4" ]; then
  oneup4=1
  sudo cp $mude/ipv4.cf $mude/ipv4 > $log
fi
if [ ! -e "$mude/ipv6" ]; then
  oneup6=1
  sudo cp $mude/ipv6.cf $mude/ipv6 > $log
fi

#更新動作
diff -q $mude/ipv4 $mude/ipv4.cf > $log
if [ $? != 0 -o $oneup4 == 1 ]; then
    sudo sed -i 's/^/        set_real_ip_from &/g' ${mude}/ipv4 > $log
    sudo sed -i 's/$/; #cfipv4&/g' $mude/ipv4 > $log
    sudo sed -i '/#cfipv4/d' ${nginx} > $log
    # 此處手動更改
    sudo sed -i '/#cfip4/r /root/cfip/ipv4' ${nginx} > $log
    updata=1
fi
diff -q $mude/ipv6 $mude/ipv6.cf > $log
if [ $? != 0 -o $oneup6 == 1 ]; then
    sudo sed -i 's/^/        set_real_ip_from &/g' ${mude}/ipv6 > $log
    sudo sed -i 's/$/; #cfipv6&/g' ${mude}/ipv6 > $log
    sudo sed -i '/#cfipv6/d' ${nginx} > $log
    # 此處手動更改
    sudo sed -i '/#cfip6/r /root/cfip/ipv6' ${nginx} > $log
    updata=1
fi

#文件更新後重載Nginx服務
if [ $updata == 1 ]; then
    sudo systemctl reload nginx > $log
fi

#重新歸檔數據已備下次判斷
sudo cp $mude/ipv4.cf $mude/ipv4 > $log
sudo cp $mude/ipv6.cf $mude/ipv6 > $log
exit 0

 

參考相關網頁: