NGINX+TOMCAT+SPRINGMVC Get user Access IP
1.Nginx Reverse Proxy
modifying Nginx configuration Files
Location/ { *********** before code *******; Proxy_set_header host $host; Proxy_set_header X-forwarded- for $proxy _add_x_forwarded_for; // set the proxy IP header, the parameters when the code gets Proxy_set_header x-real-ip // allow the request header sent to the proxy server to be redefined or add some fields to show the real client's IP}
Adding methods in 2.SPRINGMVC code
//add httpservletrequest request parameter to the Springmvc methodString IP = request.getheader ("X-forwarded-for"); if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Proxy-client-ip"); } if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Wl-proxy-client-ip"); } if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP=request.getremoteaddr (); } Utils.log.info ("IP:" + IP + "" ");//log printing in your own code
Nginx Reverse proxy +tomcat+springmvc get user access IP