久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

基于java配置nginx獲取真實IP代碼實例

瀏覽:112日期:2022-08-24 13:23:22

1、java代碼

/** 獲取客戶端IP */ public static final String getClientIp(HttpServletRequest request) { String ip = request.getHeader('X-Forwarded-For'); if (StringUtils.isBlank(ip) || 'unknown'.equalsIgnoreCase(ip)|| '127.0.0.1'.equalsIgnoreCase(ip)) { ip = request.getHeader('Proxy-Client-IP'); } if (StringUtils.isBlank(ip) || 'unknown'.equalsIgnoreCase(ip)|| '127.0.0.1'.equalsIgnoreCase(ip)) { ip = request.getHeader('WL-Proxy-Client-IP'); } if (StringUtils.isBlank(ip) || 'unknown'.equalsIgnoreCase(ip)|| '127.0.0.1'.equalsIgnoreCase(ip)) { ip = request.getHeader('X-Real-IP'); } if (StringUtils.isBlank(ip) || 'unknown'.equalsIgnoreCase(ip)|| '127.0.0.1'.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } if (StringUtils.isBlank(ip) ||'127.0.0.1'.equals(ip)|| ip.indexOf(':') > -1) { try {ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) {ip = null; } } // 對于通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照’,’分割 if (ip != null && ip.length() > 15) { if (ip.indexOf(',') > 0) {ip = ip.substring(0, ip.indexOf(',')); } } return ip; }

2、nginx需要進行相應修改,重點 proxy_set_header

server { listen xxxx; server_name 127.0.0.1; # 靜態頁面目錄 root xxxxxxxxxx; # 默認首頁 index login.html; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods ’GET,POST’; add_header Access-Control-Allow-Headers ’DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization’; #proxy_cookie_path /* /*; client_max_body_size 100m; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers ’DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization’; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; ...... } }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 探索| 阳信县| 霍林郭勒市| 东明县| 游戏| 三河市| 清水县| 夏津县| 泗洪县| 吴旗县| 栖霞市| 天气| 英山县| 双鸭山市| 剑川县| 高淳县| 东方市| 故城县| 怀宁县| 蓬安县| 疏附县| 峡江县| 东源县| 旺苍县| 新平| 汽车| 山丹县| 遵义市| 简阳市| 荔波县| 儋州市| 奉化市| 惠东县| 瑞金市| 兰坪| 正安县| 淮安市| 湟源县| 株洲市| 磴口县| 新丰县|