เป็นธรรมดาที่เราจะเอา server ไปไว้หลัง CDN อย่าง Cloudflare เพื่อประโยชน์หลาย ๆ อย่าง เช่น ลดภาระการทำงานของเครื่องของเรา ป้องกัน DDOS เข้ามาที่เครื่องของเราตรง ๆ แต่ถ้าจำเป็นต้องรู้ว่าเข้ามา ip ไหนบ้าง ip ที่เราจะได้ในกรณีนี้จะเป็น ip ของคลาวแฟร์หรือ CDN ที่เราใช้แทนที่จะเป็น ip client เพราะ CDN จะทำตัวเป็น proxy ดึงข้อมูล จ่ายข้อมูลบางส่วนแทน
- สร้าง config nginx เช่น /etc/nginx/sites-available/CDN_Cloudflare.conf มีเนื้อหา
set_real_ip_from {CDN ip};
จะบอกว่า ip ของ Cloudflare หรือ CDN มีอะไรบ้างreal_ip_header {CDN header};
ให้เอา ip ที่ Cloudflare ส่งมาทาง header CF-Connecting-IP - โดยสามารถหา ip cdn มาได้จากคู่มือของ cdn นั้น ๆ เช่น Cloudflare จะประกาศไว้ที่
https://www.cloudflare.com/ips-v4 และ
https://www.cloudflare.com/ips-v6 และ
Cloudflare HTTP headers - ใส่เนื้อหาในไฟล์ตามตัวอย่าง
# CDN Cloudflare # real CDN IP addresses # - IPv4 https://www.cloudflare.com/ips-v4 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/13; set_real_ip_from 104.24.0.0/14; 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; # - IPv6 https://www.cloudflare.com/ips-v6 set_real_ip_from 2400:cb00::/32; set_real_ip_from 2405:8100::/32; set_real_ip_from 2405:b500::/32; set_real_ip_from 2606:4700::/32; set_real_ip_from 2803:f800::/32; set_real_ip_from 2a06:98c0::/29; set_real_ip_from 2c0f:f248::/32; # Use the CF-Connecting-IP header to get the real IP real_ip_header CF-Connecting-IP;
- สร้าง symbolic link โดยคำสั่ง เช่น
sudo ln -s /etc/nginx/sites-available/CDN_Cloudflare.conf /etc/nginx/sites-enabled/CDN_Cloudflare.conf
- เทส config nginx โดยคำสั่ง
sudo systemctl reload nginx
nginx -t - restart nginx โดยคำสั่ง
sudo systemctl restart nginx
- ลองเปิด log ดูใน /var/log/nginx พวก access.log