How does the server get the client ip address?

now the front end of the website is written in vue, and the client access server forwards it to the vue page through nginx, and then the vue page visits the tomcat server. In fact, the vue page sends a request to tomcat. At this time, the IP address of the client obtained by tomcat is the vue page request address, and finally 127.0.0.1. How can I get the real ip address of the client in this case?

Apr.02,2021

generally, nginx needs location segment plus
to get real ip.
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
of course, it also depends on the actual situation

shouldn't the client request nginx to get the vue page, and then the vue page requests nginx, and then ngix forwards the request to tomcat,tomcat to return data? It's not good for vue to access tomcat directly through nginx.

the big brother above talked about the configuration of nginx. I provide the code to get the ip address in java. Refer to

.
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.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)||ip.startsWith("10.")) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  
Menu