1. 程式人生 > >nginx代理,tomcat部署服務器,後端獲取客戶端真實ip

nginx代理,tomcat部署服務器,後端獲取客戶端真實ip

nginx代理 獲取真實ip

1、環境部署說明

後端部署在tomcat服務器上,前端用nginx做代理訪問

tomcat部署目錄

技術分享圖片

nginx配置:

upstream wcfront{
    server  localhost:8991;//後臺接口
}

server {
    listen       8998;//h5訪問接口
    server_name  192.168.2.37;
    charset utf-8;

    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;



     location ^~ /fs/ {
        alias  /var/zkbc/fs/;
    }

   
    location = / {
        root   /opt/nlcn/backend/wcfront/www;//h5頁面路徑
        index  index.html index.htm;
    }

    location = /index {
        root   /opt/nlcn/backend/wcfront/www;
        rewrite ^(.*) /;
    }

    location ~ .*\.(html)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }

     location ~ .*(css)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }
    

    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff|woff2|svg|ttf)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }

    location / {
        proxy_pass http://wcfront;
        proxy_set_header Host $host:$server_port;
    }

}

2、當前配置後端獲取ip一直未127.0.0.1,現在需求是能夠獲取客戶端的ip

(1)在nginx配置上加如下配置,註意:在location /下加

 location / {
        proxy_pass http://wcfront;
        proxy_set_header Host $host:$server_port;
        proxy_set_header   Remote_Addr        $remote_addr;
        proxy_set_header   X-Real-IP          $remote_addr;
        proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
    }

(2)重啟nginx

nginx -s reload

ps -ef | grep nginx 可以查看nginx的啟動狀態及啟動時間

(3)配置tomcat

service.xml 下找到 pattern="%h %l %u %t "%r" %s %b" />

%h 修改成 %{X-Real-IP}i

重啟服務

(4) java獲取客戶端ip的方式

public static String getIpAddress(HttpServletRequest request) {
        String ip = null;

        //X-Forwarded-For:Squid 服務代理
        String ipAddresses = request.getHeader("X-Forwarded-For");

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //Proxy-Client-IP:apache 服務代理
            ipAddresses = request.getHeader("Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //WL-Proxy-Client-IP:weblogic 服務代理
            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //HTTP_CLIENT_IP:有些代理服務器
            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            //X-Real-IP:nginx服務代理
            ipAddresses = request.getHeader("X-Real-IP");
        }

        //有些網絡通過多層代理,那麽獲取到的ip就會有多個,一般都是通過逗號(,)分割開來,並且第一個ip為客戶端的真實IP
        if (ipAddresses != null && ipAddresses.length() != 0) {
            ip = ipAddresses.split(",")[0];
        }

        //還是不能獲取到,最後再通過request.getRemoteAddr();獲取
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

3、結論

需要註意的是這種方式獲取的是客戶端所在網絡的外網地址,而不是客戶端的真實ip。

例如

多個終端都在同一個局域網訪問,獲取的ip為同一個網關地址。

手機4g訪問,獲取的ip也不是手機的實際ip,而是網絡ip,例如獲取的ip是61.158.147.109,但實際手機ip是61.158.147.*(同網段另外一個ip地址),不是特別清楚手機ip和109什麽關系,有興趣的朋友可以研究研究。

nginx代理,tomcat部署服務器,後端獲取客戶端真實ip