ตั้งค่า Real IP (Restore Visitor IP) เมื่อใช้ Cloudflare กับ WordPress

ปัญหา IP ของ Cloudflare แทน IP ผู้เข้าชม

เมื่อใช้ Cloudflare เป็น Reverse Proxy สำหรับ WordPress บนเซิร์ฟเวอร์ Cloud VPS ของ de.co.th จะมีปัญหาที่สำคัญ คือ WordPress และ PHP จะบันทึก IP Address ของ Cloudflare แทน IP จริงของผู้เข้าชมเว็บไซต์ ปัญหานี้ส่งผลต่อความถูกต้องของ Analytics, Security Plugin, Geo-targeting Feature และ Spam Detection

สาเหตุของปัญหาคือ Request ไหลจาก Client ไปยัง Cloudflare ก่อน จากนั้น Cloudflare ต่อ Request ไปยัง Origin Server (WordPress บนเซิร์ฟเวอร์ Cloud VPS) ดังนั้น Nginx หรือ Apache ที่ตั้งอยู่บนเซิร์ฟเวอร์เห็น IP Source คือ IP ของ Cloudflare เท่านั้น ไม่สามารถรู้ IP ที่แท้จริงได้

วิธีแก้ไขพื้นฐาน: HTTP Header CF-Connecting-IP

Cloudflare จัดเตรียมวิธีแก้ไขโดยเพิ่ม HTTP Header ชื่อ CF-Connecting-IP ลงใน Request ทุกครั้งที่ส่งต่อไปยัง Origin Server ซึ่ง Header นี้จะเก็บ IP จริงของผู้เข้าชม ตอนนี้ปัญหาคือจะให้ Nginx/Apache และ PHP รู้ว่าต้องใช้ Header นี้แทน IP Source ปกติได้อย่างไร

สำหรับเซิร์ฟเวอร์ WordPress บนเซิร์ฟเวอร์ Cloud VPS ของ de.co.th มีวิธีแก้ไขหลายวิธี ตั้งแต่การตั้งค่า Web Server Configuration ไปจนถึงการใช้ Plugin WordPress ซึ่งเรามีจะสรุปไว้ด้านล่าง

ตั้งค่า Nginx ให้ได้ Real IP

หากเซิร์ฟเวอร์ Cloud VPS ใช้ Nginx ให้ทำการแก้ไข Configuration File ที่มักเป็น /etc/nginx/nginx.conf หรือไฟล์ใน /etc/nginx/conf.d/ เพิ่มบรรทัดต่อไปนี้:

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/12;
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;
real_ip_header CF-Connecting-IP;

บรรทัดเหล่านี้บอก Nginx ว่า ถ้า Request มาจาก IP Range ที่ระบุ (ซึ่งคือ IP Range ของ Cloudflare ทั่วโลก) ให้ใช้ Header CF-Connecting-IP เป็น IP จริง หลังจากแก้ไขไฟล์ ทำการตรวจสอบ Configuration ด้วยคำสั่ง:

nginx -t

ถ้า Output บอกว่า “configuration file test is successful” ให้รีโหลด Nginx:

systemctl reload nginx

หลังรีโหลดแล้ว WordPress บนเซิร์ฟเวอร์ Cloud VPS ของคุณจะเริ่มบันทึก IP จริงของผู้เข้าชมแทน IP ของ Cloudflare

ตั้งค่า Apache ให้ได้ Real IP

ถ้าเซิร์ฟเวอร์ WordPress Cloud VPS ใช้ Apache ต้องใช้ Module ที่ชื่อ mod_remoteip ก่อนอื่นตรวจสอบว่า Module นี้ติดตั้งหรือไม่โดยรันคำสั่ง:

apache2ctl -M | grep remoteip

ถ้าไม่มี ให้ติดตั้งด้วยคำสั่ง:

a2enmod remoteip

หลังจากเปิดใช้ Module แล้ว สร้างไฟล์ Configuration ใหม่ /etc/apache2/conf-available/cloudflare-ip.conf และเพิ่มเนื้อหา:

RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/12

จากนั้นเปิดใช้ Configuration File:

a2enconf cloudflare-ip

ทดสอบ Configuration:

apache2ctl configtest

ถ้า Output บอกว่า “Syntax OK” ให้รีสตาร์ท Apache:

systemctl restart apache2

ใช้ WordPress Plugin เพื่อแก้ไข Real IP

หากคุณไม่สะดวกแก้ไข Configuration File บนเซิร์ฟเวอร์ Cloud VPS โดยตรง มีทางเลือก คือ ใช้ WordPress Plugin ที่จัดการเรื่องนี้ให้อัตโนมัติ

Plugin ที่แนะนำคือ “Cloudflare” Plugin ของ Cloudflare เอง ซึ่งทำหน้าที่แก้ไข Real IP โดยอัตโนมัติ ติดตั้งจาก WordPress Plugin Directory เลือก Plugins แล้ว Add New ค้นหา “Cloudflare” แล้ว Install และ Activate

Plugin นี้จะอ่าน CF-Connecting-IP Header และบังคับให้ WordPress ใช้ IP นี้แทน IP Source ปกติ ทำงานได้เร็วและง่าย ไม่ต้องแก้ไข Configuration File

ตั้งค่า PHP เพื่อใช้ Real IP

บางครั้ง PHP ยังคง Cache IP เก่าไว้ หรือการตั้งค่า Web Server ยังไม่สมบูรณ์ ในกรณีนี้ให้เพิ่มโค้ดต่อไปนี้ที่ด้านบนของ WordPress wp-config.php:

if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

โค้ดนี้จะอ่าน CF-Connecting-IP Header และแทนที่ค่า $_SERVER['REMOTE_ADDR'] ซึ่งเป็นตัวแปรที่ WordPress และ Plugin ใช้ในการบันทึก IP

ตรวจสอบว่า Real IP ทำงานหรือไม่

หลังจากตั้งค่าแล้ว วิธีตรวจสอบว่า Real IP ทำงานหรือไม่คือ เข้าไป WordPress Log ดู IP ที่ถูกบันทึก ผู้ดูแลระบบสามารถใช้ Plugin เช่น “IP Geo Block” เพื่อดู IP ของผู้เข้าชมปัจจุบันตรงจาก WordPress Dashboard

หรือไปที่ Plugin ที่รายงาน IP เช่น “Wordfence Security” ก็จะแสดง IP จริงของผู้เข้าชม ถ้าตัวเลข IP ตรงกับประเทศหรือพื้นที่ของผู้เข้าชมจริงๆ แสดงว่า Real IP ทำงานได้

ส่วนเพิ่มเติม: IPv6 Support

หากเซิร์ฟเวอร์ WordPress Cloud VPS ของ de.co.th รองรับ IPv6 ต้องแน่ใจว่า Configuration Nginx/Apache ด้านบนรวม IPv6 Range ของ Cloudflare ไว้ด้วย ตัวอักษรดีว่าได้ระบุ IPv6 Ranges ใน Configuration ด้านบนแล้ว เช่น 2400:cb00::/32 เป็นต้น

สรุป

การตั้งค่า Real IP บนเซิร์ฟเวอร์ WordPress Cloud VPS ของ de.co.th ที่ใช้ Cloudflare เป็นปัญหาสำคัญที่ต้องจัดการ ด้วยการแก้ไข Nginx Configuration, Apache Configuration, หรือใช้ WordPress Plugin คุณจะสามารถให้ WordPress บันทึก IP จริงของผู้เข้าชมแทน IP ของ Cloudflare ทำให้ Analytics, Security Plugin และฟีเจอร์อื่นๆ ทำงานได้อย่างถูกต้อง