當在架設網站時,時常會幫網站前面架設一層 cdn(cloudflare),好處是 cdn 作為第一層接收使用者端的請求既可以隱藏伺服器真實 ip,更可以加速各地區的網站載入速度。
反之,多了一層 cdn 可能讓你更難存取到真實的使用者資訊,比如導致在網頁伺服器看到的 access.log 其中 $remote_addr
都是來自 cdn 廠商的 ip,例如:162.158.162.90
{ "ip": "162.158.162.90", "city": "Singapore", "region": "Singapore", "country": "SG", "loc": "1.2897,103.8501", "org": "AS13335 Cloudflare, Inc.", "postal": "018989", "timezone": "Asia/Singapore", "readme": "https://ipinfo.io/missingauth" }%
造成這個問題的原因,可以參考以下圖片的流程:

原來的使用者 IP 為: 1.1.1.1
/ 2.2.2.2
由於經過 cloudflare 的 cdn 伺服器(4.4.4.4
)存取後,導致 Logs 充斥 4.4.4.4
的 $remote_addr
為了解決這個問題
cloudflare 官方有提供在各網頁伺服器該如何設定正確的 mods 解決此問題
- 新增
nginx-cloudflase-realip
設定
# vim /etc/nginx/conf.d/nginx-cloudflase-realip.conf set_real_ip_from 173.245.48.0/20; 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 141.101.64.0/18; set_real_ip_from 108.162.192.0/18; set_real_ip_from 190.93.240.0/20; set_real_ip_from 188.114.96.0/20; set_real_ip_from 197.234.240.0/22; set_real_ip_from 198.41.128.0/17; set_real_ip_from 162.158.0.0/15; set_real_ip_from 104.16.0.0/13; set_real_ip_from 104.24.0.0/14; set_real_ip_from 172.64.0.0/13; set_real_ip_from 131.0.72.0/22; 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 2a06:98c0::/29; set_real_ip_from 2c0f:f248::/32; # (repeat for all Cloudflare IPs listed at https://www.cloudflare.com/ips/) # use any of the following two real_ip_header CF-Connecting-IP; # real_ip_header X-Forwarded-For
2. 重新讀取 nginx 設定
sudo service nginx reload
設定大功告成 ~ 重新檢查 access.log 的 $remote_addr
是否後就會正確抓到使用者 ip 了
Leave a Reply