当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx作为反向代理时传递客户端IP的设置方法

Nginx作为反向代理时传递客户端IP的设置方法

2019年04月21日  | 移动技术网网络运营  | 我要评论

nginx默认配置文件里面是没有进行日志转发配置的,这个需要我们自己手动来操作了,当然后端的real server不同时操作方法是不一样的,这里我们分别例举几种情况来说明一下。

nginx做前端,转发日志到后端nginx服务器:

因为架构的需要采用多级 nginx 反向代理,但是后端的程序获取到的客户端 ip 都是前端 nginx 的 ip,问题的根源在于后端的 nginx 在 http header 中取客户端 ip 时没有取对正确的值。
同样适用于前端是 squid 或者其他反向代理的情况。

首先前端的 nginx 要做转发客户端 ip 的配置:

location / {
proxy_pass http://localhost:8000;
 
# forward the user's ip address to rails
proxy_set_header x-real-ip $remote_addr;
# needed for https
# proxy_set_header x_forwarded_proto https;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header host $host;
proxy_redirect off;
}

后端的 nginx 需要安装一个 module: ,编译的时候默认不包含此 module,需要重新编译安装 nginx,configure 的时候加上 –with-http_realip_module,nginx 升级或者添加/删除 module 时支持,可以避免中断服务。

升级后配置 nginxhttprealipmodule,set_real_ip_from 就是指前端 nginx 或者 squid 的 ip:

location / {
proxy_pass http://localhost:8000;
 
# forward the user's ip address to rails
proxy_set_header x-real-ip $remote_addr;
# needed for https
# proxy_set_header x_forwarded_proto https;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header host $host;
proxy_redirect off;
# nginxhttprealipmodule
set_real_ip_from 192.168.1.0/24;
set_real_ip_from 192.168.2.1;
real_ip_header x-real-ip;
}

最后记得 reload nginx config

nginx做前端,转发日志到后端apache服务器:

apache日志中默认有%h来指定来访客户端你的ip地址,但是使用了nginx代理上网则%h获得的ip地址会不准。
这就需要对nginx 和apache的配置文件设定 x-forwarded-for 参数来获取客户端真实的ip地址。对于使用了反向代理的客户端,跟踪真实的ip地址。
/usr/nginx/conf/nginx.conf 添加以下参数:

proxy_set_header host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header x-forwarded-host $server_name;
proxy_set_header x-real-ip $remote_addr;

同时修改:

server {
   listen 80;
   server_name 域名 ;
   proxy_redirect off; 
    location / {
     proxy_set_header  x-forwarded-for $remote_addr;
     proxy_set_header  x-forwarded-host $server_name;
     proxy_set_header host $host;
     proxy_pass http://域名;
   }
   access_log off;
 }

重启nginx使配置生效。
 
apache端需要安装一个第三方模块"mod_rpaf"了, 官方网站:

wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz 
tar zxvf mod_rpaf-0.6.tar.gz 
cd mod_rpaf-0.6 
/opt/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

修改apache配置 /usr/apache2/conf/httpd.conf

loadmodule rpaf_module modules/mod_rpaf-2.0.so 
rpafenable on 
rpafsethostname on 
rpafproxy_ips ip地址  #nginx所在服务器的ip
rpafheader x-forwarded-for

重启apache 查看日志就可以看见日志中已经获得到真实ip了。

nginx做前端,转发日志到后端iis服务器:

  iis 如果放在反向代理后面,日志里的ip是反向代理服务器的ip,不是真正用户的ip,想要记录用户的ip要做两件事。

1.在反向代理设置x-forwarded-for段,以下为nginx下的配置示例:

server
{
  location
  {
  ……
  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  ……
  }
}

2.在iis站点上安装下面这个isapi filter,这东西是在f5的开发论坛上找到的,按开发者的话说,是为了解决iis放在f5后记录不到用户ip的问题,-_-# 管他前端是f5还是nginx还是squid还是haproxy。都可以用。应该不错。装完之后重启下iis就搞定了。
   
   回头看下iis的日志,里面的ip已经是用户的真实ip了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网