Through Nginx reverse proxy, will not get the real IP, is to obtain the Nginx IP, to get the real IP to be configured nginx configuration file: nginx.conf
Proxy_set_header X-real-ip $remote _addr;
For example:
######################################################################## #要转发地域名: Upstream t.csdn.com { server 192.168.1.188:8080 max_fails=0 weight=1; #8080为tomcat端口 }################################################################## server { Listen ; server_name t.csdn.com; Access_log/data/wwwlogs/access_tomcat.log combined; Root/usr/local/tomcat/webapps; Index index.html index.jsp; #反向代理配置, all requests for http://hostname are forwarded to the target server defined in upstream. Location /{ #此处配置的域名必须与upstream的域名一致 to forward. proxy_pass http://t.csdn.com; Proxy_set_header x-real-ip $remote _addr; } #启用nginx Status Monitor page location/nginxstatus { stub_status on; Access_log on; } }
Then Tomcat gets the way: Java
private static String Getremoteaddrip (HttpServletRequest request) {string Ipfromnginx = GetHeader (Request, "X-real-ip") ; Log.info ("Ipfromnginx:" + Ipfromnginx); Log.info ("getremoteaddr:" + request.getremoteaddr ()); return Stringutils.isempty (Ipfromnginx)? REQUEST.GETREMOTEADDR (): Ipfromnginx;} private static string GetHeader (HttpServletRequest request, String headname) {String value = Request.getheader (headname) ; return (Stringutils.isnotblank (value) &&! " Unknown ". Equalsignorecase (value))? Value: "";}
Finally, call Getremoteaddrip this method to get the IP:
String ClientIP = Getremoteaddrip (Request), Log.info ("Client IP:" + clientip);
The above describes the Nginx reverse proxy, Tomcat to obtain a real client IP instead of the server IP, including the Tomcat,nginx aspects of the content, I hope that the PHP tutorial interested in a friend helpful.