How does nginx obtain the real client ip

As a reverse proxy server, nginx acts as a proxy for our server. The following describes how to configure nginx to obtain the real client ip

1. Configure nginx.con


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://127.0.0.1:8080;
           root   html;
           index  index.html index.htm;

        #获取真实ip配置
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

2. In the java program, it can be obtained in the following ways:

    @GetMapping(value = "/getRealIp")
    public String getRealIp(){
        log.info(request.getRemoteAddr());
        log.info(request.getRequestURL().toString());
        log.info("端口" + request.getRemotePort() + "");


        log.info("======================");
        log.info("真实ip:{}",getClientIP());

        return request.getRemoteAddr() + "\n" +
                request.getRequestURL();

    }

 /***
     * 获取客户端IP地址;这里通过了Nginx获取;X-Real-IP
     */
    public String getClientIP() {
        String fromSource = "X-Real-IP";
        String ip = request.getHeader("X-Real-IP");
        log.info("X-Real-IP 初始化值的ip:{} ", ip);
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
            fromSource = "X-Forwarded-For";
            log.info("X-Forwarded-For赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            fromSource = "Proxy-Client-IP";
            log.info("Proxy-Client-IP赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            fromSource = "WL-Proxy-Client-IP";
            log.info("WL-Proxy-Client-IP赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            fromSource = "request.getRemoteAddr";
            log.info("RemoteAddr赋值成功ip:{} ", ip);
//        }
        return ip;
    }

In this way, the real ip can be printed out! That is, the value of request.getHeader("X-Real-IP")

Quote:

Check the port occupation and release the occupied port_Query the port number of Google Chrome_JDSYDWR's Blog-CSDN Blog

https://blog.51cto.com/u_14020077/5836635

[Network] Why is the ip found by Baidu different from that found by ipconfig; detailed explanation of public network IP and private network ip; detailed explanation of network classification ABC;_wx5bc47e97d0ded's technical blog_51CTO blog

Guess you like

Origin blog.csdn.net/weixin_42767099/article/details/129994908