免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

nginx代理,tomcat部署服務器,后端獲取客戶端真實i-創新互聯

1、環境部署說明

網站建設哪家好,找創新互聯!專注于網頁設計、網站建設、微信開發、微信平臺小程序開發、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了靈川免費建站歡迎大家使用!

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

tomcat部署目錄

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

nginx配置:

upstream wcfront{     server  localhost:8991;//后臺接口 } server {     listen       8998;//h6訪問接口     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頁面路徑         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什么關系,有興趣的朋友可以研究研究。

另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前標題:nginx代理,tomcat部署服務器,后端獲取客戶端真實i-創新互聯
分享路徑:http://m.newbst.com/article22/dephjc.html

成都網站建設公司_創新互聯,為您提供小程序開發品牌網站設計、全網營銷推廣、微信小程序、Google、ChatGPT

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站網頁設計