Nginx透传获取客户端IP地址_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Nginx透传获取客户端IP地址

Nginx透传获取客户端IP地址

 2013/9/25 13:05:48  greatwqs  程序员俱乐部  我要评论(0)
  • 摘要:nginx.conf配置:location/{proxy_passhttp://127.0.0.1:8080/myweb/;proxy_redirectoff;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}myweb后端获取方式:/****获取客户端IP地址;这里通过了Nginx获取;X-Real
  • 标签:Nginx 客户 客户端

nginx.conf配置:

?

class="java" name="code">location / { 
     proxy_pass http://127.0.0.1:8080/myweb/;
     proxy_redirect    off;
     proxy_set_header  Host             $host;
     proxy_set_header  X-Real-IP        $remote_addr;
     proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
}

?myweb后端获取方式:

?

/***
 * 获取客户端IP地址;这里通过了Nginx获取;X-Real-IP,
 * @param request
 * @return
 */
public static String getClientIP(HttpServletRequest request) {
	String fromSource = "X-Real-IP";
	String ip = request.getHeader("X-Real-IP");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("X-Forwarded-For");
		fromSource = "X-Forwarded-For";
	}
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("Proxy-Client-IP");
		fromSource = "Proxy-Client-IP";
	}
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("WL-Proxy-Client-IP");
		fromSource = "WL-Proxy-Client-IP";
	}
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getRemoteAddr();
		fromSource = "request.getRemoteAddr";
	}
	appLog.info("App Client IP: "+ip+", fromSource: "+fromSource);
	return ip;
}

?参考:?http://www.iteye.com/topic/1124492

发表评论
用户名: 匿名