HTTP 請求經過多層代理 (CDN、Load Balancer、Reverse Proxy) 時,原始客戶端 IP 會被附加在特定 Header 中。標準檢測順序:
HTTP_X_FORWARDED_FOR (可能包含多個 IP,逗號分隔,**最左邊為原始客戶端**)HTTP_X_REAL_IP (Nginx/Apache 常用)HTTP_CLIENT_IP (部分 Proxy)HTTP_X_CLUSTER_CLIENT_IP (AWS ALB 等)REMOTE_ADDR (直接連線或最後一層 Proxy IP)<%
Function GetRealIP()
Dim ip, forwarded, realIP, clientIP, clusterIP, remoteAddr, ips, i
' 1. X-Forwarded-For (標準)
forwarded = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If forwarded <> "" Then
ips = Split(forwarded, ",")
For i = 0 To UBound(ips)
ip = Trim(ips(i))
If IsValidPublicIP(ip) Then
GetRealIP = ip
Exit Function
End If
Next
End If
' 2. X-Real-IP (Nginx proxy_set_header)
realIP = Request.ServerVariables("HTTP_X_REAL_IP")
If realIP <> "" And IsValidPublicIP(realIP) Then
GetRealIP = realIP
Exit Function
End If
' 3. Client-IP
clientIP = Request.ServerVariables("HTTP_CLIENT_IP")
If clientIP <> "" And IsValidPublicIP(clientIP) Then
GetRealIP = clientIP
Exit Function
End If
' 4. X-Cluster-Client-IP (AWS ALB)
clusterIP = Request.ServerVariables("HTTP_X_CLUSTER_CLIENT_IP")
If clusterIP <> "" And IsValidPublicIP(clusterIP) Then
GetRealIP = clusterIP
Exit Function
End If
' 5. 回退至 REMOTE_ADDR
remoteAddr = Request.ServerVariables("REMOTE_ADDR")
If IsValidPublicIP(remoteAddr) Then
GetRealIP = remoteAddr
Else
GetRealIP = "0.0.0.0" ' 未知
End If
End Function
' 驗證是否為合法公網 IPv4 (簡易版,排除私有/保留區段)
Function IsValidPublicIP(ip)
Dim octets, o1, o2
If Not IsIPv4Format(ip) Then IsValidPublicIP = False: Exit Function
octets = Split(ip, ".")
o1 = CInt(octets(0))
o2 = CInt(octets(1))
' 私有網段 (RFC 1918)
If o1 = 10 Then IsValidPublicIP = False: Exit Function
If o1 = 172 And o2 >= 16 And o2 <= 31 Then IsValidPublicIP = False: Exit Function
If o1 = 192 And o2 = 168 Then IsValidPublicIP = False: Exit Function
' 回環/保留
If o1 = 127 Then IsValidPublicIP = False: Exit Function
If o1 = 169 And o2 = 254 Then IsValidPublicIP = False: Exit Function ' Link-local
If o1 >= 224 Then IsValidPublicIP = False: Exit Function ' Multicast/保留
IsValidPublicIP = True
End Function
Function IsIPv4Format(ip)
Dim re, matches
Set re = New RegExp
re.Pattern = "^(\d{1,3}\.){3}\d{1,3}$"
IsIPv4Format = re.Test(ip)
If IsIPv4Format Then
Dim parts, p
parts = Split(ip, ".")
For Each p In parts
If CInt(p) > 255 Then IsIPv4Format = False: Exit Function
Next
End If
End Function
%>
<%
Dim realIP
realIP = GetRealIP()
Response.Write "真實 IP: " & realIP & "
"
Response.Write "REMOTE_ADDR: " & Request.ServerVariables("REMOTE_ADDR") & "
"
Response.Write "X-Forwarded-For: " & Request.ServerVariables("HTTP_X_FORWARDED_FOR") & "
"
%>
<%
' 假設已安裝 IP2Location COM 元件
Dim ip2loc, record
Set ip2loc = Server.CreateObject("IP2Location.Component")
ip2loc.Open Server.MapPath("/bin/IP2LOCATION.BIN")
Set record = ip2loc.IPQuery(GetRealIP())
If Not record Is Nothing Then
Response.Write "國家: " & record.CountryLong & "
"
Response.Write "城市: " & record.City & "
"
Response.Write "ISP: " & record.ISP & "
"
End If
%>
| 架構層 | 需設定的 Header | 範例 (Nginx) |
|---|---|---|
| CDN (Cloudflare) | CF-Connecting-IP | 自動添加,ASP 讀取 HTTP_CF_CONNECTING_IP |
| Load Balancer (AWS ALB) | X-Forwarded-For, X-Forwarded-Proto | 自動添加 |
| Reverse Proxy (Nginx) | proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | 在 location / 區塊設定 |
| IIS ARR | X-Forwarded-For | Server Farm → Proxy → 勾選「使用 X-Forwarded-For」 |
X-Forwarded-For 可被用戶端偽造!
僅信任**你直接控制的那一層 Proxy** 添加的 Header。例如:Nginx 設定 `proxy_set_header X-Real-IP $remote_addr;` 後,ASP 只信任 `HTTP_X_REAL_IP`;不信任客戶端直接送來的 `X-Forwarded-For`。
最佳做法:在最邊緣 (CDN/Load Balancer) 將真實 IP 寫入**自訂 Header** (如 `X-Original-Client-IP`),並刪除用戶端送來的同名 Header,後端只讀取該自訂 Header。