kaola4549 发表于 2018-11-12 09:29:14

Nginx如何保留真实IP和获取前端IP

server 10.20.10.102:80;  server 10.20.10.103:80;
  }
  server
  {
  listen 80;
  server_name www.test.com;
  index index.html index.htm index.jsp default.jsp index.do default.do;
  root /home/www/game;
  error_page 404 = http://www.test.com;
  if (-d $request_filename)
  {
  rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  }
  #转发动态页面给Tomcat处理
  location ~ \.(jsp|jspx|do)?$ {
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://tomcat_server;
  2,tomcat应用工程(网站程序配置)
  用 String ip = request.getHeader("X-Real-IP");替代String ip = request.getRemoteAddr();
  这是程序这款的了
  -----------------------------------------------------------------------------------------------------------
  apache+nginx静态和动态分离,nginx在apache前,做的PROXY来转发请求到内部的apache上
  1,nginx配置:
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  2,apache配置:
  用mod_rpaf来获取IP的
  所以需要安装这个模块
  下载:http://stderr.net/apache/rpaf/download/
  tar zxvf mod_rpaf-0.6.tar.gz
  cd mod_rpaf-0.6
  /usr/local/www/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
  配置apache:
  在 httpd.conf中添加
  LoadModule rpaf_module libexec/apache2/mod_rpaf-2.0.so
  RPAFenable On
  RPAFsethostname On
  RPAFproxy_ips 192.168.1.1 #这个是前段的IP,可不是后端的IP哦
  RPAFheader X-Forwarded-For
  注意,不仅仅要配置nginx,后端获取也需要做配置,例如上面两个例子都做了相应的配置
  -----------------------------------------------------------------------------------------------------------
  nginx+nginx或squid+nginx,也就是说nginx是后端的web服务器,前端用nginx或squid做了缓存服务器,那么nginx如何接受的呢,这里注意了,nginx既可以给后端保留真实IP,也可以作为后端服务器接受前端给的真实IP,他是如何接受的你
  nginx通过http_realip_module模块来实现的
  这需要重新编译,如果提前编译好了就无需重新编译了
  1,重新编译nginx
  编译
  ./configure --prefix=/usr/local/nginx --without-http_autoindex_module --without-http_geo_module --without-http_map_module --without-http_browser_module --with-http_stub_status_module --with-http_realip_module --with-pcre=../pcre-8.12

页: [1]
查看完整版本: Nginx如何保留真实IP和获取前端IP