国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - Nginx - 基于nginx反向代理獲取用戶真實Ip地址詳解

基于nginx反向代理獲取用戶真實Ip地址詳解

2022-03-10 17:18中遙TXT首席工程師 Nginx

我們訪問互聯網上的服務時,大多數時客戶端并不是直接訪問到服務端的,而是客戶端首先請求到反向代理,反向代理再轉發到服務端實現服務訪問,這篇文章主要給大家介紹了關于如何基于nginx反向代理獲取用戶真實Ip地址的相關資料

引言

nginx做反向代理時,默認的配置后端獲取到的Ip地址都來自于nginx,用request.getRemoteAddr();獲取到的是nginx的ip地址,而不是用戶的真實ip.

1.修改Nginx配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
    listen       80;
    server_name  jenkins.local.com;
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://192.168.10.204:8899;  
     }
     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
        root   html;
        index  index.html index.htm index.jsp index.action default.html;
     }
       proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

在原來的基礎配置上加上后三行配置,就可以使用request.getHeader(“x-forwarded-for”)來獲取用戶真實的Ip地址了

2.java獲取客戶端Ip

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.zimax.cqyf.admin.util;
 
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
 
/**
 * http工具類
 */
public class HttpUtils {
    /**
     * 獲取真實的ip
     * @param request
     * @return
     * @throws UnknownHostException
     */
    public static String getRealIp(HttpServletRequest request){
        String ip;
        // 有的user可能使用代理,為處理用戶使用代理的情況,使用x-forwarded-for
        if  (request.getHeader("x-forwarded-for") == null)  {
            ip = request.getRemoteAddr();
        else  {
            ip = request.getHeader("x-forwarded-for");
        }
        if  ("127.0.0.1".equals(ip))  {
            try {
                // 獲取本機真正的ip地址
                ip = InetAddress.getLocalHost().getHostAddress();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return ip;
    }
}

附:一個ip工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import javax.servlet.http.HttpServletRequest;
/**
* IP地址工具類
* @author xudongdong
*
*/
public class IpUtil {
    
    /**
     * 私有化構造器
     */
    private IpUtil() {
    }
    /**
     * 獲取真實IP地址
     * <p>使用getRealIP代替該方法</p>
     * @param request req
     * @return ip
     */
    @Deprecated
    public static String getClinetIpByReq(HttpServletRequest request) {
        // 獲取客戶端ip地址
        String clientIp = request.getHeader("x-forwarded-for");
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("Proxy-Client-IP");
        }
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        /*
         * 對于獲取到多ip的情況下,找到公網ip.
         */
        String sIP = null;
        if (clientIp != null && !clientIp.contains("unknown") && clientIp.indexOf(",") > 0) {
            String[] ipsz = clientIp.split(",");
            for (String anIpsz : ipsz) {
                if (!isInnerIP(anIpsz.trim())) {
                    sIP = anIpsz.trim();
                    break;
                }
            }
            /*
             * 如果多ip都是內網ip,則取第一個ip.
             */
            if (null == sIP) {
                sIP = ipsz[0].trim();
            }
            clientIp = sIP;
        }
        if (clientIp != null && clientIp.contains("unknown")){
            clientIp =clientIp.replaceAll("unknown,", "");
            clientIp = clientIp.trim();
        }
        if ("".equals(clientIp) || null == clientIp){
            clientIp = "127.0.0.1";
        }
        return clientIp;
    }
    
    /**
     * 判斷IP是否是內網地址
     * @param ipAddress ip地址
     * @return 是否是內網地址
     */
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp;
        long ipNum = getIpNum(ipAddress);
        /**  
        私有IP:A類  10.0.0.0-10.255.255.255  
               B類  172.16.0.0-172.31.255.255  
               C類  192.168.0.0-192.168.255.255  
        當然,還有127這個網段是環回地址  
        **/
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.255.255.255");
        
        long bBegin = getIpNum("172.16.0.0");
        long bEnd = getIpNum("172.31.255.255");
        
        long cBegin = getIpNum("192.168.0.0");
        long cEnd = getIpNum("192.168.255.255");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd)
                || ipAddress.equals("127.0.0.1");
        return isInnerIp;
    }
    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);
        return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
    }
    
    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }
    public static String getRealIP(HttpServletRequest request){
        // 獲取客戶端ip地址
        String clientIp = request.getHeader("x-forwarded-for");
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        String[] clientIps = clientIp.split(",");
        if(clientIps.length <= 1) return clientIp.trim();
        // 判斷是否來自CDN
        if(isComefromCDN(request)){
            if(clientIps.length>=2) return clientIps[clientIps.length-2].trim();
        }
        return clientIps[clientIps.length-1].trim();
    }
    private static boolean isComefromCDN(HttpServletRequest request) {
        String host = request.getHeader("host");
        return host.contains("www.189.cn") ||host.contains("shouji.189.cn") || host.contains(
                "image2.chinatelecom-ec.com") || host.contains(
                "image1.chinatelecom-ec.com");
    }
}

總結

到此這篇關于基于nginx反向代理獲取用戶真實Ip地址的文章就介紹到這了,更多相關nginx獲取用戶真實Ip內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/sll714827/article/details/123332269

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成a人片在线观看 | 日韩精品第一页 | 日韩中文字幕av | 国产精品入口在线观看 | 欧美精品欧美精品系列 | 日日夜夜精品视频 | 亚洲成人毛片 | 欧美成人影院 | 免费在线观看黄 | 亚洲网站在线 | 2023国产精品久久久精品双 | 日韩大片在线观看 | 国产亚洲欧美在线 | 搡女人真爽免费午夜网站 | 色婷婷综合久久 | 国产综合久久 | 国产综合一区二区 | 欧美成人伊人 | 天天干夜夜操 | 三级成人在线 | 在线一级黄色片 | 91麻豆精品国产91久久久久久久久 | 成人自拍视频 | 日本天天操 | 一级在线看| 国产三级在线 | 午夜影院免费 | 国产欧美日韩二区 | 一区在线播放 | 国产精品视频播放 | 成人在线观看日韩 | 成人国产精品156免费观看 | 狠狠的日 | 国产精品中文字幕在线观看 | 国产中文在线 | 亚洲男人第一天堂 | 97成人精品视频在线观看 | 亚洲欧美视频在线观看 | 天天干天天骑 | 99久久毛片免费观看 | 婷婷精品久久久久久久久久不卡 |