问题描述
系统是用ssh开发的,其中当用户通过Apache访问跳转到tomcat,struts取到的ip值永远是127.0.0.1,直接通过:8080端口访问取到的ip值是正确的,apache 中http.conf配置ProxyPass /images !ProxyPass /css !ProxyPass /js !ProxyPass /cms http://localhost:8080/cms另外还试过跳转<Location /cms>ProxyPass http://localhost:8080/cmsProxyPassReverse http://localhost:8080/cms</Location>jk没试过,看了网上配置都出错较郁闷,请问大家是怎么处理的
解决方案
我也是用apache+tomcat的运行环间,我是用下边的代码取的客户端IP,没发现有错的:)/** * 如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个, * 而是一串Ip值,究竟哪个才是真正的用户端的真实IP呢?答案是取X-Forwarded-For * 中第一个非unknown的有效IP字符串。如:X- Forwarded-For:192.168.1.110, * 192.168.1.120, 192.168.1.130, 192.168.1.100用户真实IP为: 192.168.1.110 * * @param request * @return */public static String getIpAddr(HttpServletRequest request) {String 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();}return ip;}
解决方案二:
好像都是用jk的多,使用代理转发的方式,肯定得不到原来的IP了。