Nginx 使用realip模块获取真实IP

Nginx代理将真实IP透传,Nginxweb打印真实IP;

 

ngx_http_realip 模块的作用是当你的 nginx 服务器位于一个反向代理后面时,去获取客户端真实访问的 IP。这个模块默认没有编译到 Nginx 中,需要在编译时通过 --with-http_realip_module 开启。

一、编译安装首先得安装该模块  -with-http_realip_module

/configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

二、参数配置

提供代理服务的Nginx配置,在location模块添加;

proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

提供web服务的Nginx配置,在http模块、location 模块根据自己需求添加;

set_real_ip_from  192.168.6.80;        #这个ip是代理服务器的地址;
real_ip_header    X-Forwarded-For;

配置nginx的log_format ,添加“http_x_forwarded_for”;

   log_format  main  '"$http_x_forwarded_for" - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';

在访问日志时候就会打印真实的IP地址;

参数解析:

    set_real_ip_from: address | CIDR | unix:, 定义信任地址(trusted address),配合后续的 real_ip_recursive 使用。
    real_ip_header: field | X-Real-IP | X-Forwarded-For | proxy_protocol, 默认值是 X-Real-IP, 定义真实的 IP 在请求头中的字段名。
    real_ip_recursive: on | off, 默认值是 off,如果是 off,则使用 real_ip_header 请求头中匹配信任地址(trusted address)中最后一个作为客户端请求的真实 IP;如果是 on , 则取匹配的前一个值作为客户端真实 IP。

ngx_http_realip 模块将用获取到的客户端真实 IP 作为 Nginx 内置变量 $remote_addr 的值。这样你又能通过 $remote_addr 得到你客户端访问的真实 IP 了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值