NGINX配置获取CloudFlare 下的访客真实IP并记录到日志


我用的是lnmp.org的环境

/usr/local/nginx/conf/nginx.conf

    http {

    }

部分增加

map $HTTP_CF_CONNECTING_IP  $clientRealIp {
    ""    $remote_addr;
    ~^(?P<firstAddr>[0-9.]+),?.*$    $firstAddr;
}
log_format  main  '$clientRealIp [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '$http_user_agent $remote_addr $request_time';

主要是为了通用性,如果关闭了CDN,可以不需要修改获取IP的方式,所以才这么修改,不然直接用$HTTP_CF_CONNECTING_IP就行了(这个时候就不需要在日志格式里使用$clientRealIp)

然后在网站记录的日志定义使用main这个日志格式

比如

access_log /home/wwwlogs/abc.com.log main;

参考来源:https://www.bnxb.com/php/27592.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM