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... }
nginx
2022-11-28 17Nginx是一个高性能的HTTP和反向代理web服务器,也是站长常见的网站环境之一.nginx凭借高性能低消耗近几年越来越多站长使用,学习好nginx也成为站长必备知识点.我的...
- Nginx禁止下载指定后缀文件配置方法 [2023-10-02]
- Nginx指定域名访问请求 [2023-09-27]
- Nginx只允许Cloudflare IP访问获取真实IP [2023-08-12]
- nginx获取header头信息教程 [2023-04-10]
- Nginx下载大文件超时失败解决方法 [2023-04-05]