web server will obtain the real address of the requesting client

Obtaining the client or server requests a web page, acquires the IP requires attention, because before a request reaches the server, usually one or more layers through a proxy server, such as a reverse proxy server http://192.168.1.10:port/ when the reverse proxy URL into the URL http://www.xxx.com/ with request.getRemoteAddr () IP address acquisition method is: 127.0.0.1 or 192.168.1.10, and not the client's real IP. But after a proxy server, the request header will be some more fields, we can get the real IP based on these fields rather than through the proxy server generated IP.

    String ip = request.getHeader("X-Forwarded-For");
    if (StringUtils.isNotEmpty(ip) && !"unKnow".equalsIgnoreCase(ip)) {
        Have multiple values ​​// ip repeatedly reverse proxy, ip is the first real ip
        String[] ipList = ip.split(",");
        ip = ipList[0];
     } else {
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isEmpty(ip) || "unKnown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
     }

REMOTE_ADDR
IP address represents the requesting remote host, remote_addr on behalf of the client's IP, but its value is not provided by the client, but the server based on ip specified client when your browser to visit a Web site, it is assumed without any intermediate agents, then the site's web server (Nginx, Apache, etc.) will put remote_addr set your machine IP, if you use a proxy, then your browser will first access the proxy, and then from this Acting forwarded to the site so that the web server will set the IP remote_addr this proxy machine
x_forwarded_for
XFF short head, it represents a client-side HTTP request that is true IP, only this time will be added through the HTTP proxy or server load balancing, as mentioned above, when you use a proxy, web server I do not know your real IP, in order to avoid this situation, the proxy server will usually increase header called x_forwarded_for, the customer IP connection to its end (ie your Internet machine IP) added to the header information, the so to ensure that the site can get to the real web server IP
General format:
1
X-Forwarded-For: 1.1.1.1, 2.2.2.2, 3.3.3.3
1.1.1.1 Representative request is issued by, after three agents, a first layer is a 2.2.2.2, 3.3.3.3 is the second layer, and the source of this request is a third layer of IP4.4.4.4 agent
X-Real-IP
When there are multiple agents, you can configure "proxy_set_header X-Real-IP $ remote_addr" on the first reverse proxy  this time by X-Real-IP acquire real client IP  
-----------------------------------------
X-Forwarded-For each of a generally non-transparent proxy will forward the request upstream server's IP address appended to the X-Forwarded-For, comma divided
X-Real-IP agent is generally the last one of the upstream IP address header added to the
X-Forwarded-For a plurality of IP addresses, and the X-Real-IP is that there is a specific value of the proxy server can be configured.

Guess you like

Origin www.cnblogs.com/doit8791/p/11809908.html