Home > Backend Development > PHP Tutorial > tomcat gets the real user IP of Nginx

tomcat gets the real user IP of Nginx

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:06:39
Original
1028 people have browsed it

Required: Add proxy_set_header X-Real-IP $remote_addr;

upstream tomcat_server { 
            server localhost:8080 weight=1;
    } 

    server {
        listen       80;
        server_name 192.168.40.116;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	location ~* "\.(jsp|do)$" { #当请求的是jsp或do文件时直接到tomcat上去取
     	 	#root  html;
      	 	#index index.html index.htm;
         	#proxy_pass http://192.168.18.201/;
          	proxy_pass http://tomcat_server;
		client_max_body_size     2048m;
       		client_body_buffer_size  128k;
       		proxy_connect_timeout    600;
       		proxy_read_timeout       600;
       		proxy_send_timeout       6000;
       		proxy_buffer_size        16k;
       		proxy_buffers            64k;
       		proxy_busy_buffers_size 64k;
       		proxy_temp_file_write_size 64k;
        }

        location / {
           #发布目录/data/www 
        #    root   /home/ding/ITsoftware/tomcat/webapps/100mshCloud;
	#   fastcgi_pass   192.168.40.116:8080;
  	  <strong> proxy_set_header  X-Real-IP  $remote_addr;  </strong>
 	   proxy_pass http://tomcat_server/; #注释默认两行,新增一行。
        #    index  index.html index.htm;
        }
Copy after login
tomcat Get:

public static String getRemoteAddrIp(HttpServletRequest request) {  
    String ipFromNginx = getHeader(request, "X-Real-IP");  
    System.out.println("ipFromNginx:" + ipFromNginx);  
    System.out.println("getRemoteAddr:" + request.getRemoteAddr());  
    return StringUtil.isEmpty(ipFromNginx) ? request.getRemoteAddr() : ipFromNginx;  
}  
  
  
private static String getHeader(HttpServletRequest request, String headName) {  
    String value = request.getHeader(headName);  
    return !StringUtils.isBlank(value) && !"unknown".equalsIgnoreCase(value) ? value : "";  
}  
Copy after login

The above introduces how tomcat obtains the real user IP of Nginx, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template