Nginx只允许Cloudflare IP访问获取真实IP
我的站长站
2023-08-12
共人阅读
Nginx设置只允许Cloudflare IP访问后获取真实访客IP,跟普通套Cloudflare获取真实访客IP的方法不一样。
要使用map指令将客户端的 IP 存储到变量(ie$real_client_ip)中,并在日志中使用该变量:
# 这些放在配置文件头部---
map $http_x_forwarded_for $real_client_ip {
~^(\d+\.\d+\.\d+\.\d+) $1;
default $http_cf_connecting_ip;
}
# replace the default '$remote_addr' with the '$real_client_ip'
log_format custom_log_format '$real_client_ip - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_host" "$upstream_response_time"'
'"$http_referer" "$http_user_agent"';
# 这些放在配置文件头部---
server {
listen 80;
listen [::]:80;
server_name example.com;
include /etc/nginx/allow-cloudflare-only.conf;
#在生成日志文件后面加上custom_log_format
access_log /var/log/nginx/access.log custom_log_format;
#...the rest of your configs here...
}相关专题
Cloudflare
2023-08-07 568Cloudflare是全球热门免费CDN服务,具备无限防御与节点加速能力,广泛用于网站安全防护。本站整理全套Cloudflare实操教程与配置方法,帮助站长合理部署防护策略,抵御网...
- Cloudflare免费套餐大全 [2026-06-20]
- cloudflare优选ip自定义主机名TXT验证失败解决方法 [2025-07-25]
- Cloudflare自选IP常见问题分享 [2025-07-25]
- 添加cloudflare DNS API令牌教程 [2025-02-16]
- 绕过Cloudflare的403禁止报错教程 [2024-10-22]

