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 209Cloudflare是一款世界级热门CDN,Cloudflare支持免费使用,无限CDN防御,是全球许多网站防御的必备cdn工具.学好使用Cloudflare,可以帮助你的服务器免受网络攻击.我...
- 宝塔面板添加cloudflare DNS API令牌教程 [2025-02-16]
- 绕过Cloudflare的403禁止报错教程 [2024-10-22]
- enhanced-FaaS-in-China最新提升访问速度项目 [2024-09-23]
- CloudFlare CDN自动刷新缓存插件 [2024-09-12]
- Cloudflare DNS解析服务使用方法 [2024-09-08]


