在nginx中,您可以使用以下几种方式来获取客户端源IP:

  1. 使用 $remote_addr 变量:$remote_addr 变量包含客户端的IP地址。您可以在nginx的配置文件中使用该变量来获取客户端的源IP。
location / {
    set $real_ip $remote_addr;
    ...
}
  1. 使用 $http_x_forwarded_for 变量:当请求通过代理服务器时,$http_x_forwarded_for 变量包含一个逗号分隔的IP地址列表,其中最左边的IP地址就是客户端的源IP。
location / {
    set $real_ip $http_x_forwarded_for;
    ...
}
  1. 使用 $http_x_real_ip 变量:如果您的代理服务器已经配置了 proxy_set_header X-Real-IP $remote_addr;,那么您可以使用 $http_x_real_ip 变量来获取客户端源IP。
location / {
    set $real_ip $http_x_real_ip;
    ...
}

请注意,使用第2和第3种方式时,您需要确保代理服务器已经正确地配置了相应的请求头。此外,使用 $http_x_forwarded_for$http_x_real_ip 变量获取的IP地址可能是不可信的,因为这些变量可以被伪造。因此,您应该谨慎使用这些变量

nginx 获取客户端源ip 有哪些手段

原文地址: https://www.cveoy.top/t/topic/iMbK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录