HttpServletRequest获取nginx代理后用户真实IP

#step 1:vim /etc/nginx/nginx.conf

location / {
            proxy_pass http://127.0.0.1:8081;
            proxy_set_header   Host(别名)             $host;
            proxy_set_header   X-real-iP(别名)        $remote_addr;
            proxy_set_header   X-Forwarded-For(别名)  $proxy_add_x_forwarded_for;
        }

#step 2:重启nginx

方法一:进入nginx可执行目录sbin下(which nginx),输入命令./nginx -s reload 即可。

方法二:查找当前nginx进程号,然后输入命令:kill -HUP 进程号,实现重启nginx服务。

#step 3:从HttpServletRequest的Header中获取

//服务器域名
String Host = request.getHeader("Host");
//(方式一)来访者公网IP
String X-real-ip = request.getHeader("X-real-ip");
//(方式二)来访者公网IP
String X-Forwarded-For = request.getHeader("X-Forwarded-For");
//WEB应用IP(127.0.0.1)
String getRemoteAddr = request.getRemoteAddr();

 

你可能感兴趣的:(HttpServletRequest获取nginx代理后用户真实IP)