詳解Nginx獲取客戶端真實IP
為什么需要獲取真實IP
在現代 Web 架構中,為了應對大量用戶訪問,網站通常會部署在以下架構之后:
? WAF(如 Cloudflare WAF、AWS WAF)
? 負載均衡器(如 AWS ALB/ELB、阿里云 SLB)
? CDN 服務(如 Cloudflare、AWS CloudFront)
? 反向代理(如 Nginx、Apache)
因此,一個典型的客戶端請求流程如下:
真實用戶(1.2.3.4)CDN/WAF(103.x.x.x)負載均衡器(10.0.1.100)Nginx應用服務器如果沒有特殊配置,Nginx 的訪問日志只能記錄到直接連接的上游設備 IP(負載均衡器或 CDN 的 IP),而不是真實用戶的 IP 地址。這會導致:
? 日志分析困難:無法識別真實用戶行為
? 安全防護失效:無法基于用戶 IP 進行訪問控制
? 地域限制失效:無法根據用戶地理位置提供服務
? 監控告警不準確:無法準確識別攻擊來源
技術原理
HTTP 頭部傳遞機制
負載均衡器和 CDN 通常會在 HTTP 請求頭中添加真實用戶的 IP 信息:
頭部字段 | 使用場景 | 示例 |
| 標準字段,大多數代理使用 |
|
| 簡單代理環境 |
|
| Cloudflare CDN 專用 |
|
| RFC 7239 標準 |
|
關于上述字段的詳細介紹,可以參考:
? X-Forwarded-For
? CF-Connecting-IP
? Forwarded
? RFC 7239
連接來源 IP vs 真實客戶端 IP
連接來源 IP
? 定義:與 Nginx 建立 TCP 連接的設備 IP
? 獲取方式:通過系統調用 getpeername() 獲得
? 特點:無法被應用層偽造,是真實的網絡層信息
? 在 Nginx 中:對應 $realip_remote_addr 變量
真實客戶端 IP
? 定義:最終用戶的實際 IP 地址
? 獲取方式:從 HTTP 頭部解析
? 特點:可能被中間代理修改或偽造
? 在 Nginx 中:經過處理后的 $remote_addr 變量
核心配置指令
可以參考Nginx Real IP Module Documentation
real_ip_header
作用:指定從哪個 HTTP 頭部獲取真實 IP
# 常用配置
real_ip_header X-Forwarded-For; # 標準配置
real_ip_header X-Real-IP; # 簡單代理
real_ip_header CF-Connecting-IP; # Cloudflare CDNset_real_ip_from
作用:定義可信任的代理 IP 范圍(白名單機制)
# 基本語法
set_real_ip_from ip_address;
set_real_ip_from ip_address/netmask;
set_real_ip_from unix:;
# 實際示例
set_real_ip_from 10.0.0.0/8; # 內網段
set_real_ip_from 172.16.0.0/12; # 私有網絡
set_real_ip_from 192.168.1.100; # 特定 IPreal_ip_recursive
作用:啟用遞歸處理多層代理
real_ip_recursive on; # 啟用(推薦)
real_ip_recursive off; # 禁用(默認)工作機制詳解
白名單驗證機制
set_real_ip_from 采用白名單機制:
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;工作流程
1. 檢查連接來源 IP
2. 如果來源 IP 在白名單中 → 信任請求 → 解析 HTTP 頭部
3. 如果來源 IP 不在白名單中 → 不信任請求 → 忽略 HTTP 頭部
示例對比
來源 IP 在白名單中:
連接來源: 10.0.1.100 (? 在 10.0.0.0/8 范圍內)
X-Forwarded-For: 1.2.3.4
結果: $remote_addr = 1.2.3.4來源 IP 不在白名單中:
連接來源: 8.8.8.8 (? 不在白名單范圍內)
X-Forwarded-For: 1.2.3.4
結果: $remote_addr = 8.8.8.8 (忽略頭部信息)多值 X-Forwarded-For 處理算法
X-Forwarded-For 格式
X-Forwarded-For: client_ip, proxy1_ip, proxy2_ip, proxy3_ip
←────────── 從左到右:客戶端到服務器 ──────────→非遞歸模式 real_ip_recursive off
選擇 最后一個(最右邊) IP:
set_real_ip_from 192.168.1.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive off;
# X-Forwarded-For: 1.2.3.4, 172.16.1.10, 192.168.1.50
# 選擇: 192.168.1.50遞歸模式 real_ip_recursive on
從右到左檢查,跳過可信 IP,選擇第一個不可信 IP:
set_real_ip_from 192.168.1.0/24; # 可信代理1
set_real_ip_from 172.16.1.0/24; # 可信代理2
real_ip_header X-Forwarded-For;
real_ip_recursive on;
# X-Forwarded-For: 1.2.3.4, 172.16.1.10, 192.168.1.50
# 處理過程:
# 1. 檢查 192.168.1.50 → 可信,跳過
# 2. 檢查 172.16.1.10 → 可信,跳過
# 3. 檢查 1.2.3.4 → 不可信,選擇
# 結果: $remote_addr = 1.2.3.4不同匹配程度的處理邏輯
假設 X-Forwarded-For: 1.2.3.4, 203.0.113.5, 172.16.1.10,連接來源為 172.16.1.10:
情況1:只匹配最后一個
set_real_ip_from 172.16.0.0/12; # 只信任 172.16.1.10
# 處理: 172.16.1.10(跳過) → 203.0.113.5(選擇)
# 結果: $remote_addr = 203.0.113.5情況2:匹配最后兩個
set_real_ip_from 172.16.0.0/12; # 信任 172.16.1.10
set_real_ip_from 203.0.113.0/24; # 信任 203.0.113.5
# 處理: 172.16.1.10(跳過) → 203.0.113.5(跳過) → 1.2.3.4(選擇)
# 結果: $remote_addr = 1.2.3.4情況3:全部匹配
set_real_ip_from 172.16.0.0/12; # 信任 172.16.1.10
set_real_ip_from 203.0.113.0/24; # 信任 203.0.113.5
set_real_ip_from 1.2.3.0/24; # 信任 1.2.3.4
# 處理: 所有IP都可信 → 選擇最左邊的IP
# 結果: $remote_addr = 1.2.3.4除了上述情況,還有其他情況,比如:
情況4:連接來源不可信,也就是沒有任何一個IP在可信列表中
set_real_ip_from 172.16.0.0/12; # 信任 172.16.1.10
# 但連接來源是: 8.8.8.8 (不在可信列表中)
# X-Forwarded-For: 1.2.3.4, 203.0.113.5, 172.16.1.10
# 處理: 連接來源不可信 → 完全忽略 X-Forwarded-For 頭部
# 結果: $remote_addr = 8.8.8.8實際場景配置
AWS ALB/ELB 環境
http {
# AWS ALB 通常使用 VPC 內網 IP
set_real_ip_from10.0.0.0/8;
set_real_ip_from172.16.0.0/12;
set_real_ip_from192.168.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursiveon;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen80;
access_log /var/log/nginx/access.log main;
location / {
# 應用邏輯
}
}
}Cloudflare CDN 環境
http {
# Cloudflare IP 段(需要定期更新)
set_real_ip_from173.245.48.0/20;
set_real_ip_from103.21.244.0/22;
set_real_ip_from103.22.200.0/22;
set_real_ip_from103.31.4.0/22;
set_real_ip_from141.101.64.0/18;
set_real_ip_from108.162.192.0/18;
set_real_ip_from190.93.240.0/20;
set_real_ip_from188.114.96.0/20;
set_real_ip_from197.234.240.0/22;
set_real_ip_from198.41.128.0/17;
# 使用 Cloudflare 專用頭部
real_ip_header CF-Connecting-IP;
real_ip_recursiveon;
server {
listen80;
# 獲取用戶國家信息(Cloudflare 提供)
location / {
add_header X-Country-Code $http_cf_ipcountry;
# 應用邏輯
}
}
}混合環境(CDN + 負載均衡器)
http {
# CDN 層 IP 段
set_real_ip_from103.21.244.0/22; # Cloudflare
set_real_ip_from104.16.0.0/12; # Cloudflare
# 負載均衡器層
set_real_ip_from10.0.0.0/8; # AWS VPC
set_real_ip_from172.31.0.0/16; # AWS Default VPC
# 內網代理層
set_real_ip_from192.168.100.0/24; # 內網代理
real_ip_header X-Forwarded-For;
real_ip_recursiveon;
# 詳細日志格式
log_format detailed '$realip_remote_addr forwarded $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
server {
listen80;
access_log /var/log/nginx/access.log detailed;
location / {
# 應用邏輯
}
}
}內網多層代理環境
http {
# 第一層:外部負載均衡器
set_real_ip_from172.16.1.0/24;
# 第二層:內網負載均衡器
set_real_ip_from10.0.1.0/24;
# 第三層:應用網關
set_real_ip_from192.168.1.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursiveon;
server {
listen80;
# 基于真實 IP 的訪問控制
location /admin {
allow192.168.0.0/16; # 允許內網訪問
allow10.0.0.0/8; # 允許 VPN 訪問
deny all;
# 管理接口
}
# 基于真實 IP 的限流
location /api {
limit_req zone=per_ip burst=10 nodelay;
# API 接口
}
}
}
# 限流配置
limit_req_zone$remote_addr zone=per_ip:10m rate=10r/s;多值處理算法
算法實現偽代碼
def nginx_select_real_ip(forwarded_for_header, trusted_proxies, recursive_mode, connection_ip):
# 首先檢查連接來源是否可信
if connection_ip notin trusted_proxies:
return connection_ip # 不可信連接,直接返回連接 IP
# 解析 X-Forwarded-For 頭部
ifnot forwarded_for_header:
return connection_ip
ips = [ip.strip() for ip in forwarded_for_header.split(',')]
ifnot recursive_mode:
# 非遞歸模式:返回最后一個 IP
return ips[-1]
else:
# 遞歸模式:從右到左找第一個不可信的 IP
for ip inreversed(ips):
if ip notin trusted_proxies:
return ip
# 如果所有 IP 都是可信的,返回最左邊的 IP
return ips[0]處理示例
示例 1:標準多層代理
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 103.21.244.0/22;
real_ip_recursive on;這樣配置后,一個實際的舉例以及處理過程如下:
連接來源: 10.0.1.100
X-Forwarded-For: 1.2.3.4, 103.21.244.50, 172.16.1.10, 10.0.1.100
處理步驟:
1. 連接來源 10.0.1.100 ∈ 10.0.0.0/8 → 可信 ?
2. 從右到左檢查:
- 10.0.1.100 ∈ 可信列表 → 跳過
- 172.16.1.10 ∈ 可信列表 → 跳過
- 103.21.244.50 ∈ 可信列表 → 跳過
- 1.2.3.4 ? 可信列表 → 選擇 ?
結果: $remote_addr = 1.2.3.4示例 2:惡意請求檢測
連接來源: 8.8.8.8 (不在可信列表中)
X-Forwarded-For: 1.2.3.4, 10.0.1.100
處理步驟:
1. 連接來源 8.8.8.8 ? 任何可信網段 → 不可信 ?
2. 忽略 X-Forwarded-For 頭部
結果: $remote_addr = 8.8.8.8示例 3:邊界情況 - 所有 IP 都可信
連接來源: 10.0.1.100
X-Forwarded-For: 10.0.2.50, 172.16.1.10, 10.0.1.100
處理步驟:
1. 連接來源可信 ?
2. 從右到左檢查:
- 10.0.1.100 ∈ 可信列表 → 跳過
- 172.16.1.10 ∈ 可信列表 → 跳過
- 10.0.2.50 ∈ 可信列表 → 跳過
3. 所有 IP 都可信,選擇最左邊的
結果: $remote_addr = 10.0.2.50安全考慮
白名單精確性
# 好的做法:精確指定
set_real_ip_from 10.0.1.100; # 特定負載均衡器
set_real_ip_from 172.16.1.0/24; # 特定代理網段
# 危險的做法:范圍過大
set_real_ip_from 0.0.0.0/0; # 信任所有 IP - 危險做法定期更新 IP 列表
CDN 和云服務提供商的 IP 段會定期變化,需要及時更新:
#!/bin/bash
# 自動更新 Cloudflare IP 段的腳本
# 獲取最新的 Cloudflare IP 段
curl -s https://www.cloudflare.com/ips-v4 > /tmp/cloudflare-ips-v4.txt
curl -s https://www.cloudflare.com/ips-v6 > /tmp/cloudflare-ips-v6.txt
# 生成 Nginx 配置片段
echo"# Cloudflare IP ranges - Updated $(date)" > /etc/nginx/conf.d/cloudflare-ips.conf
whileread ip; do
echo"set_real_ip_from $ip;" >> /etc/nginx/conf.d/cloudflare-ips.conf
done < /tmp/cloudflare-ips-v4.txt
echo"real_ip_header CF-Connecting-IP;" >> /etc/nginx/conf.d/cloudflare-ips.conf
# 測試配置并重載
nginx -t && systemctl reload nginx多層驗證策略
http {
# 基礎 IP 驗證
set_real_ip_from10.0.0.0/8;
real_ip_header X-Forwarded-For;
real_ip_recursiveon;
# 地理位置驗證(需要 GeoIP 模塊)
geoip_country /usr/share/GeoIP/GeoIP.dat;
server {
listen80;
# 結合真實 IP 和地理位置的訪問控制
location /admin {
# 只允許來自特定國家的特定 IP 段
if ($geoip_country_code != CN) {
return403;
}
allow192.168.0.0/16;
deny all;
}
# 異常檢測:如果 X-Forwarded-For 和連接 IP 差異過大
location /sensitive {
access_by_lua_block {
local forwarded = ngx.var.http_x_forwarded_for
local remote = ngx.var.remote_addr
local connection = ngx.var.realip_remote_addr
-- 如果存在可疑的 IP 跳轉,記錄日志
if forwarded and remote ~= connection then
ngx.log(ngx.WARN, "Suspicious IP forwarding: " ..
"connection=" .. connection ..
" forwarded=" .. forwarded ..
" final=" .. remote)
end
}
}
}
}簡化方案:自定義真實 IP 頭部
問題分析
X-Forwarded-For 的復雜匹配邏輯確實容易出錯,特別是在多層代理環境中,我們經常聽到一個叫做 HTTP頭部偽造攻擊 的安全問題,因為攻擊者可以偽造 X-Forwarded-For 頭部來繞過IP限制
一個更簡單可靠的方案是:在架構最外層(如 WAF、CDN)設置自定義頭部。
解決方案
方案設計
客戶端(1.2.3.4)WAF/CDN負載均衡器Nginx設置X-Client-True-IP透傳讀取外層設置(WAF/CDN 配置)
# 在 WAF 或最外層代理設置
set $client_real_ip $remote_addr; # 獲取四層握手的真實 IP
proxy_set_header X-Client-True-IP $client_real_ip;
proxy_set_header Host $host;
proxy_pass http://backend;Nginx 端配置(極簡)
# 簡化配置:只信任自定義頭部
set_real_ip_from 10.0.0.0/8; # 內網代理段
set_real_ip_from 172.16.0.0/12; # 負載均衡器段
real_ip_header X-Client-True-IP; # 使用自定義頭部
real_ip_recursive off; # 無需遞歸處理方案對比
傳統 X-Forwarded-For 方案
# 復雜配置
set_real_ip_from103.21.244.0/22; # CDN IP段1
set_real_ip_from103.22.200.0/22; # CDN IP段2
set_real_ip_from172.16.0.0/12; # 負載均衡器
set_real_ip_from10.0.0.0/8; # 內網段
real_ip_header X-Forwarded-For;
real_ip_recursiveon; # 需要遞歸處理
# 潛在問題:
# - 需要維護多個可信 IP 段
# - 復雜的遞歸匹配邏輯
# - CDN IP 段變化需要更新自定義頭部方案
# 簡化配置
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Client-True-IP; # 單一可信頭部
real_ip_recursive off; # 無需復雜邏輯調試與驗證
調試端點配置
server {
listen80;
# IP 信息調試端點
location /debug-ip {
return200"Connection IP: $realip_remote_addr\nProcessed IP: $remote_addr\nX-Forwarded-For: $http_x_forwarded_for\nX-Real-IP: $http_x_real_ip\nUser-Agent: $http_user_agent\n";
add_header Content-Type text/plain;
}
# 詳細的調試信息
location /debug-detailed {
access_by_lua_block {
local headers = ngx.req.get_headers()
local info = {
connection_ip = ngx.var.realip_remote_addr,
processed_ip = ngx.var.remote_addr,
forwarded_for = headers["x-forwarded-for"],
real_ip = headers["x-real-ip"],
cf_connecting_ip = headers["cf-connecting-ip"],
user_agent = headers["user-agent"],
timestamp = ngx.time()
}
ngx.header.content_type = "application/json"
ngx.say(require("cjson").encode(info))
}
}
}日志分析配置
http {
# 詳細的日志格式
log_format debug_ip '$time_local '
'connection_ip=$realip_remote_addr '
'processed_ip=$remote_addr '
'forwarded="$http_x_forwarded_for" '
'real_ip="$http_x_real_ip" '
'cf_ip="$http_cf_connecting_ip" '
'request="$request" '
'status=$status '
'user_agent="$http_user_agent"';
server {
access_log /var/log/nginx/debug_ip.log debug_ip;
}
}測試腳本
#!/bin/bash
# 測試真實 IP 獲取的腳本
NGINX_HOST="your-nginx-server.com"
TEST_ENDPOINT="/debug-ip"
echo"=== 測試 1: 直接訪問 ==="
curl -H "X-Forwarded-For: 1.2.3.4" http://$NGINX_HOST$TEST_ENDPOINT
echo -e "\n=== 測試 2: 多層代理模擬 ==="
curl -H "X-Forwarded-For: 1.2.3.4, 172.16.1.10, 10.0.1.100" http://$NGINX_HOST$TEST_ENDPOINT
echo -e "\n=== 測試 3: 惡意偽造檢測 ==="
curl -H "X-Forwarded-For: 192.168.1.1" http://$NGINX_HOST$TEST_ENDPOINT
echo -e "\n=== 測試 4: 無頭部信息 ==="
curl http://$NGINX_HOST$TEST_ENDPOINT監控腳本
#!/bin/bash
# 監控真實 IP 獲取異常的腳本
LOG_FILE="/var/log/nginx/access.log"
ALERT_THRESHOLD=100
# 檢查可疑的 IP 跳轉
suspicious_count=$(tail -10000 $LOG_FILE | grep -c "forwarded.*differ")
if [ $suspicious_count -gt $ALERT_THRESHOLD ]; then
echo"ALERT: Detected $suspicious_count suspicious IP forwarding in recent logs"
# 發送告警
fi
# 檢查 IP 地理位置異常
geoip_anomaly=$(tail -10000 $LOG_FILE | awk '{print $1}' | sort | uniq -c | sort -nr | head -10)
echo"Top IP addresses by frequency:"
echo "$geoip_anomaly"最佳實踐
生產環境標準配置
http {
# 真實 IP 獲取配置
include /etc/nginx/conf.d/real-ip.conf;
# 限流配置
limit_req_zone$remote_addr zone=per_ip:10m rate=10r/s;
limit_req_zone$server_name zone=per_server:10m rate=100r/s;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format security '$time_local $remote_addr $realip_remote_addr '
'"$request" $status "$http_user_agent"';
server {
listen80;
server_name example.com;
access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/security.log security;
# 安全頭部
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
# 基于真實 IP 的限流
limit_req zone=per_ip burst=20 nodelay;
location / {
# 應用邏輯
}
# 管理接口訪問控制
location /admin {
allow192.168.0.0/16;
allow10.0.0.0/8;
deny all;
# 管理功能
}
}
}真實 IP 配置文件
可單獨放置一個文件/etc/nginx/conf.d/real-ip.conf
# 內網 IP 段
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 192.168.0.0/16;
# 云服務商 IP 段(需要定期更新)
include /etc/nginx/conf.d/cloudflare-ips.conf;
include /etc/nginx/conf.d/aws-ips.conf;
# 配置真實 IP 頭部
real_ip_header X-Forwarded-For;
real_ip_recursive on;常見問題
配置后仍然顯示代理 IP ?
可能原因:
1. 代理 IP 不在 set_real_ip_from 范圍內
2. HTTP 頭部字段名稱不匹配
3. Nginx 配置未生效
解決方案:
# 1. 檢查連接來源 IP
curl -H "X-Forwarded-For: 1.2.3.4" http://your-server/debug-ip
# 2. 驗證配置語法
nginx -t
# 3. 重載配置
nginx -s reload
# 4. 檢查錯誤日志
tail -f /var/log/nginx/error.log如何處理 IPv6 地址 ?
# IPv6 地址配置
set_real_ip_from 2001:db8::/32;
set_real_ip_from ::1;
# 混合 IPv4/IPv6 環境
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 2001:db8::/32;
real_ip_header X-Forwarded-For;使用 X-Real-IP 能否正確獲取真實 IP ?
X-Real-IP 和 X-Forwarded-For 的行為不同,需要了解其特點:
X-Real-IP 的特點
? 單值頭部:只包含一個 IP,不像 X-Forwarded-For 包含多個
? 設置方:通常由簡單的反向代理或某些負載均衡器設置
? 標準化程度:相比 X-Forwarded-For 標準化程度較低
實際效果對比
假設請求鏈路:用戶(1.2.3.4) → CDN(103.x.x.x) → ALB(10.0.1.100) → Nginx
如果 ALB 設置了:
X-Real-IP: 1.2.3.4
X-Forwarded-For: 1.2.3.4, 103.21.244.50使用 X-Real-IP:
real_ip_header X-Real-IP;
set_real_ip_from 10.0.0.0/8;
# 結果: $remote_addr = 1.2.3.4 ? 正確使用 X-Forwarded-For(非遞歸):
real_ip_header X-Forwarded-For;
real_ip_recursive off;
set_real_ip_from 10.0.0.0/8;
# 結果: $remote_addr = 103.21.244.50 ? 拿到CDN IP使用建議
簡單環境(推薦 X-Real-IP):
# 只有一層代理,且確認設置了 X-Real-IP
real_ip_header X-Real-IP;
set_real_ip_from 10.0.0.0/8;
real_ip_recursive off;復雜環境(推薦 X-Forwarded-For):
# 多層代理環境
real_ip_header X-Forwarded-For;
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
real_ip_recursive on;檢查方法
# 檢查你的環境是否設置了 X-Real-IP
curl -H "Host: your-domain.com" http://your-nginx/debug-ip多個 real_ip_header 如何處理 ?
Nginx 只能配置一個 real_ip_header,但可以通過 Lua 腳本處理多個頭部:
location / {
access_by_lua_block {
-- 優先級:X-Real-IP > CF-Connecting-IP > X-Forwarded-For
local real_ip = ngx.var.http_x_real_ip
if not real_ip or real_ip == "" then
real_ip = ngx.var.http_cf_connecting_ip
end
if not real_ip or real_ip == "" then
local xff = ngx.var.http_x_forwarded_for
if xff then
real_ip = xff:match("([^,]+)") -- 取第一個IP
end
end
if real_ip then
ngx.var.client_real_ip = real_ip
end
}
proxy_set_header X-Client-IP $client_real_ip;
proxy_pass http://backend;
}如何驗證配置是否正確 ?
測試步驟:
1. 創建測試端點
2. 模擬不同來源的請求
3. 檢查日志輸出
4. 驗證安全策略
# 完整測試腳本
#!/bin/bash
SERVER="http://your-nginx-server.com"
echo"=== 測試真實 IP 獲取配置 ==="
echo"1. 測試直接訪問:"
curl -s "$SERVER/debug-ip" | grep -E "(Connection|Processed)"
echo -e "\n2. 測試代理轉發:"
curl -s -H "X-Forwarded-For: 203.0.113.1""$SERVER/debug-ip" | grep -E "(Connection|Processed)"
echo -e "\n3. 測試多層代理:"
curl -s -H "X-Forwarded-For: 203.0.113.1, 172.16.1.10""$SERVER/debug-ip" | grep -E "(Connection|Processed)"
echo -e "\n4. 測試訪問控制:"
curl -s -w "%{http_code}\n""$SERVER/admin" -o /dev/null
echo -e "\n配置測試完成!"性能影響 ?
真實 IP 獲取對性能的影響很小:
? CPU 開銷:每個請求增加 < 0.1ms 處理時間
? 內存開銷:忽略不計
? 網絡開銷:無額外網絡請求
優化建議:
# 在 http 塊中配置,避免重復解析
http {
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
# 使用高效的日志格式
log_format optimized '$remote_addr [$time_local] "$request" $status';
}附:一些常見服務商的IP地址范圍
1. Cloudflare IP Ranges
2. AWS IP Address Ranges



















