nginx使用realip模块获取用户的真实ip

2021-05-08 0 492

我们经常会使用cdn来达到加快网站访问速度和隐藏服务器真实ip的目的,但是站点使用了cdn后程序获取到的用户ip以及nginx日志中记录的ip均会变成cdn的中转ip。不过cdn一般会实用自定义ip头来保存用户的真实ip,或者是将其放在X_FORWARDED_FOR头里,通过nginx的realip模块和这些ip头里的信息就可以获取到用户的真实ip了。

首先需要确认安装nginx的时候加上了realip模块:

./configure --with-http_realip_module

nginx配置示例:

    server {
    listen       80;
    server_name  www.test.com;
    index index.php index.html index.html;
    root /data/site/www.test.com;
    access_log  /data/wwwlogs/test.access.log  main;
 
    set_real_ip_from  192.168.50.0/24;
    set_real_ip_from  61.22.22.22;
    set_real_ip_from  121.207.33.33;
    set_real_ip_from  127.0.0.1;
    real_ip_header    X-Forwarded-For;
    real_ip_recursive on;
    fastcgi_pass  unix:/var/run/phpfpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

捷丰下载 技术教程 nginx使用realip模块获取用户的真实ip https://www.nwgamer.com/308.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务