Nginx中如何获取用户真实的IP

https://blog.csdn.net/broadview2006/article/details/54570943

  • 网卡接口删除IP命令:ip a delete 192.168.1.1/24 dev ens33(你的设备名称);
  • centos6.0启动命令:service iptables start
  • netstat -luntp | grep 800;查看端口
    在这里插入图片描述
    在这里插入图片描述
  • 首先配置代理:
    在这里插入图片描述
    -在这里插入图片描述
  • 第二台虚拟机访问日志的格式:

在这里插入图片描述
在这里插入图片描述

  • 第三台虚拟机格式:

在这里插入图片描述
在这里插入图片描述

  • 第四台虚拟机格式:

在这里插入图片描述
在这里插入图片描述

  • 结论:在默认情况下,下划线忽略,所以改为了’ - ';
  • 下面是我后来在服务器上做的实验。
 location / {
        proxy_set_header Host $proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $http_add_x_forwarded_for;
        proxy_pass  http://129.204.225.131:81;
    }


然后,根据打印的日志,获取到了我的真实IP。
在这里插入图片描述

  • 根据上面,可以看到,http_x_forwarded_for还是为空。猜想,可能http_add_x_forwarded_for
    是空的,而根据官网,如果x_forwarded_for头没有出现在客户端的请求头里面,那么,这个变量的值就会等于remote_addr。
  • 对日志做修改,查看打印信息;
  • 在这里插入图片描述

虚拟机2:
在这里插入图片描述
虚拟机3:
在这里插入图片描述
官网解释:
Embedded Variables
The ngx_http_proxy_module module supports embedded variables that can be used to compose headers using the proxy_set_header directive:

$proxy_host
name and port of a proxied server as specified in the proxy_pass directive;
$proxy_port
port of a proxied server as specified in the proxy_pass directive, or the protocol’s default port;
$proxy_add_x_forwarded_for
the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章