【Nginx】【09】nginx获取用户真实ip地址

  • Post author:
  • Post category:其他




1.nignx nginx 配置

proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;


http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

    句法:proxy_set_header field value;
    默认:proxy_set_header Host $proxy_host;
    	  proxy_set_header Connection close;
    语境:http,server,location
    
    Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:
   
    允许将字段重新定义或附加到传递给代理服务器的请求标头。
    该value可以包含文本,变量,以及它们的组合。
    当且仅当proxy_set_header 在当前级别上没有定义指令时,这些指令才从先前级别继承 。默认情况下,只重新定义了两个字段:
    proxy_set_header Host  $proxy_host;
    proxy_set_header Connection close;
    
    If caching is enabled, the header fields “If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” 
    from the original request are not passed to the proxied server.An unchanged “Host” request header field can be passed like this:
    
    如果启用了缓存,则标题字段为“If-Modified-Since”,“If-Unmodified-Since”,“If-None-Match”,“If-Match”,“Range”和“If-Range”来自原始请求不会传递给代理服务器。
    未更改的“主机”请求标头字段可以像这样传递:
    proxy_set_header Host  $http_host;
    
    However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host variable - its value equals the server name in the “Host” request header field or the primary server name if this field is not present:
    但是,如果客户端请求标头中不存在此字段,则不会传递任何内容。在这种情况下,最好使用$host变量 - 其值等于“主机”请求标头字段中的服务器名称,或者如果此字段不存在则等于主服务器名称:
    proxy_set_header Host  $host;
    In addition, the server name can be passed together with the port of the proxied server:
    此外,服务器名称可以与代理服务器的端口一起传递:
    
    proxy_set_header Host  $host:$proxy_port;
    If the value of a header field is an empty string then this field will not be passed to a proxied server:
    如果标头字段的值是空字符串,则此字段将不会传递给代理服务器:
    proxy_set_header Accept-Encoding "";



2.java代码

public static String getIpAddr(HttpServletRequest request) {  
	String ip;
	int index;
	try {
		ip = request.getHeader("x-forwarded-for");  
		// Proxy-Client-IP 这个一般是经过apache http服务器的请求才会有,用apache http做代理时一般会加上Proxy-Client-IP请求头,而WL-Proxy-Client-IP是他的weblogic插件加上的头。
		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();  
		} 
		if(StringUtils.isEmpty(ip)){
			return "";
		}
		index = ip.indexOf(",");
		
		if(index != -1){
			return ip.substring(0,index);
		}else{
			return ip;
		}
	} catch (Exception e) {
		return "";
	}
}



3.解释

$remote_addr 如果是一个nginx代理这个值就可以满足,但是如果是多个nginx这个值可能是上一个nginx的ip地址

$proxy_add_x_forwarded_for 这是一个地址的链条如果经历了多个ip地址那么第一个ip地址就是游客的ip地址



版权声明:本文为huiyanshizhen21原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。