nginx如何获取用户的真实ip地址

avatar 2021年4月20日18:07:42 评论 1,114 次浏览

获取用户的真实ip地址,在日志分析的过程中能够帮助我们很好的分析用户的区域,分析用户的数量,留存等等。日志的分析可以帮助我们很好的能够分析用户的喜好,根据用户的喜好方便精准推送用户喜好信息。我们在以后的工作中也能跟据用户的爱好做长久用户输出。首先,我们要查看一下我们的nginx是否安装了--with-http_realip_module

[root@www.wulaoer.org ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.7/src/openssl-1.1.1g --with-openssl-opt='enable-weak-ssl-ciphers'

如果没有安装,我们可以通过安装的时候nginx的包在重新编译一下即可,编译后

[root@www.wulaoer.org nginx ~]# ./configure --prefix=/usr/local/nginx/   --with-http_realip_module
[root@www.wulaoer.org nginx ~]# make && make install

编译之后,我们只需要在nginx的配置文件中,给域名加获取用户ip模块。

[root@www.wulaoer.org ~]# vim /usr/local/nginx/conf/nginx.con
server {
    listen       80;
    server_name  www.wulaoer.org;
    access_log  /var/log/nginx/www.wulaoer.org.access.log main;
    error_log  /var/log/nginx/www.wulaoer.org.error.log error;
    location / {
        proxy_pass  http://wulaoer;
        proxy_set_header   Host             $host;  #添加下面三行
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        include        fastcgi_params;
    }
}

至此,nginx获取用户的真实ip地址就做完了,这里就不验证了,我这边没有安装,直接获取的。。。。。。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: