

Cấu hình Varnish hoạt động với CloudFlare và Nginx
Đầu tiên, cấu hình để Nginx phân biệt được IP Proxy từ hệ thống CloudFlare và quy định header của IP gốc. Mở file/etc/nginx/nginx.conf
và thêm đoạn sau vào block http {}
http { set_real_ip_from 204.93.240.0/24; set_real_ip_from 204.93.177.0/24; set_real_ip_from 199.27.128.0/21; set_real_ip_from 173.245.48.0/20; set_real_ip_from 103.22.200.0/22; set_real_ip_from 141.101.64.0/18; set_real_ip_from 108.162.192.0/18; real_ip_header CF-Connecting-IP;Danh sách này sẽ thường xuyên thay đổi, do đó bạn cần liên tục update từ link chính thức này. Tiếp theo, để Nginx biết được request được xử lý thông qua proxy là Varnish, chỉ cần thêm
127.0.0.1
vào danh sách proxy trong file nginx.conf
. Cấu hình hoàn chỉnh như sau:
http { set_real_ip_from 204.93.240.0/24; set_real_ip_from 204.93.177.0/24; set_real_ip_from 199.27.128.0/21; set_real_ip_from 173.245.48.0/20; set_real_ip_from 103.22.200.0/22; set_real_ip_from 141.101.64.0/18; set_real_ip_from 108.162.192.0/18; set_real_ip_from 127.0.0.1/32; real_ip_header CF-Connecting-IP;Đừng quên chỉnh sửa cấu trúc
log_format
nữa nhé:
log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';Vậy là xong, restart Nginx bạn sẽ thấy IP gốc của người dùng sẽ xuất hiện trong access.log. Chúc bạn thành công.
Theo hocvps