nginx代理,tomcat部署服務(wù)器,后端獲取客戶端真實(shí)i

1、環(huán)境部署說(shuō)明

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鄠邑免費(fèi)建站歡迎大家使用!

后端部署在tomcat服務(wù)器上,前端用nginx做代理訪問(wèn)

tomcat部署目錄

nginx代理,tomcat部署服務(wù)器,后端獲取客戶端真實(shí)i

nginx配置:

upstream wcfront{
    server  localhost:8991;//后臺(tái)接口
}

server {
    listen       8998;//h6訪問(wèn)接口
    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;//h6頁(yè)面路徑
        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、當(dāng)前配置后端獲取ip一直未127.0.0.1,現(xiàn)在需求是能夠獲取客戶端的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的啟動(dòng)狀態(tài)及啟動(dòng)時(shí)間

(3)配置tomcat

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

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

重啟服務(wù)

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

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

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

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

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

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

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

        //有些網(wǎng)絡(luò)通過(guò)多層代理,那么獲取到的ip就會(huì)有多個(gè),一般都是通過(guò)逗號(hào)(,)分割開(kāi)來(lái),并且第一個(gè)ip為客戶端的真實(shí)IP
        if (ipAddresses != null && ipAddresses.length() != 0) {
            ip = ipAddresses.split(",")[0];
        }

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

3、結(jié)論

需要注意的是這種方式獲取的是客戶端所在網(wǎng)絡(luò)的外網(wǎng)地址,而不是客戶端的真實(shí)ip。

例如

多個(gè)終端都在同一個(gè)局域網(wǎng)訪問(wèn),獲取的ip為同一個(gè)網(wǎng)關(guān)地址。

手機(jī)4g訪問(wèn),獲取的ip也不是手機(jī)的實(shí)際ip,而是網(wǎng)絡(luò)ip,例如獲取的ip是61.158.147.109,但實(shí)際手機(jī)ip是61.158.147.*(同網(wǎng)段另外一個(gè)ip地址),不是特別清楚手機(jī)ip和109什么關(guān)系,有興趣的朋友可以研究研究。

新聞名稱:nginx代理,tomcat部署服務(wù)器,后端獲取客戶端真實(shí)i
標(biāo)題URL:http://muchs.cn/article28/isjhcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站內(nèi)鏈、云服務(wù)器、電子商務(wù)、域名注冊(cè)、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)