IPパススルー
なぜIPパススルーを行います
使用してCDN
サイトを、その後、ユーザーが要求したとき、静的リソースはヒットしませんでした、スピードアップするために静的リソースを加速しないCDN
発信局に行きます、その後、ソースステーションにアクセスするための時間がコンテンツを要求したIP
ためにCDN
ノードをIP
、そのおそらく私たちを通じてだけでなく、WAF
ファイアウォールとフロントエンドのロードバランシング(SLB
バック)実際のユーザーを取得する可能性が低いIP
我々は、ユーザーのアクセスカウントするようであれば、情報IP
および領域を、それは本当ではないかもしれないので、より面倒になりIP
、ユーザーに使用するどのようなメカニズムを持っていますIP
最終のバックエンドに渡されたがアプリケーション・サーバーの仕事。
実験環境
アクセス処理
ホストコンピューター | IPの設定 | リマーク |
---|---|---|
Chrome |
10.0.0.1 |
Windows ブラウザ |
LB-01 |
10.0.0.5 |
エージェント |
LB-02 |
10.0.0.6 |
二つの薬剤 |
LB-03 |
10.0.0.7 |
3つのエージェント |
WEB |
10.0.0.8 |
WEBホスト |
一般的な方法
X-リアルタイムIP
説明
それぞれのHTTP
リクエストヘッダの参加X-Real-IP
プロキシクラスの唯一の存在は、クライアントのパラメータが実際に本当であれば、情報をIP
時間が、そこに複数のレベルのプロキシ、親のためのプロキシ情報であればIP
情報、およびユーザーは、実際に取得することはできませんIP
したがってこの方法は推奨されています。
nginxの設定
#LB-01一级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.6;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
#LB-02二级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.7;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
#LB-03三级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.8;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
#WEB主机配置
server {
listen 80;
server_name ip.test.com;
root /wwwroot;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Webホストテストスクリプト
#文件名:index.php
#放到测试目录/wwwroot内
<?php
$IP_ADDRESS = getenv("HTTP_X_REAL_IP");
echo "SOURCE IP ADDRESS: $IP_ADDRESS";
?>
注意:設定した後、すべてのすべてのコンフィギュレーションファイルを保存し、起動しnginx
たサービスを。あなたがもしwindows
テストでなければなりませんC:\Windows\System32\drivers\etc\hosts
、次のように、ファイル内のローカル解像度レコードに参加します:
10.0.0.5 ip.test.com
Wiresharkのパケットキャプチャ情報
HTTPヘッダー情報
GET / HTTP/1.1
Host: ip.test.com
X-Real-IP: 10.0.0.6
Connection: close
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: UM_distinctid=16afc8d323f94-0360d7ebcc9bae-3d644509-18e414-16afc8d324063f
クロムオープンテスト
結論:Wireshark
トレースストリームとChrome
目に見えるテスト、X-Real-IP
および実際のクライアントに得ることができないIP
場合ならば、あなたは正しいクライアントに取得することができ、アドレス、単一レベルプロキシIP
マルチレベルプロキシXiecaiがある場合は、。
X-転送さ-のために
説明
在每个HTTP
请求头中加入X-Forwarded-For
信息,若只存在1级的代理服务器,则该参数为客户端的真实IP
,若是存在多级代理时,每经过一级代理服务器,则追加上级代理服务的IP
,可以获取到真实的用户IP
,但若是遇到伪造的X-Forwarded-For
信息或第一级代理未启用X-Forwarded-For
都不能获取到真实用户IP
,此法是目前比较常用的方法,但更推荐使用nginx_http_realip_module
模块添加可信代理的方法。
nginx配置
#LB-01一级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.6;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#可以尝试注释此处的,看看最终能否获取到真实客户端IP
}
}
#LB-02二级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.7;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#LB-03三级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.8;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#WEB主机配置
server {
listen 80;
server_name ip.test.com;
root /wwwroot;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
WEB主机测试脚本
#文件名:index.php
#放到测试目录/wwwroot内
<?php
$IP_ADDRESS = getenv("HTTP_X_FORWARD_FOR");
echo "SOURCE IP ADDRESS: $IP_ADDRESS";
?>
注:记得重载nginx
配置。
Wireshark抓包信息
HTTP报头信息
GET / HTTP/1.1
Host: ip.test.com
X-Forwarded-For: 10.0.0.1, 10.0.0.5, 10.0.0.6
Connection: close
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: UM_distinctid=16afc8d323f94-0360d7ebcc9bae-3d644509-18e414-16afc8d324063f
chrome打开测试
结论:由Wireshark
追踪流和Chrome
的测试可见,X-Forwarded-For
可以获取到真实的客户端IP
地址,即便是在多级代理下,也可以获取到正确的客户端IP
,但如果某台代理未设置X-Forwarded-For
,后端应用服务器可能并不能获取到正确的客户端IP
。
nginx_http_realip_module
描述
通过预定义可信任的代理主机的IP
的方式(可信代理主机默认都会加入X-Forwarded-For
信息),根据些X-Forwarded-For
的信息,从右到左,滤除掉这些可信代理的IP
信息,最终获取到的就是真实客户端IP
信息,此信息替换$remote_addr
这个变量,使最终客户端IP
在WEB
后端应用服务器的access.log
第一列显示,方便取值分析。
nginx配置
#LB-01一级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.6;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#此为可信任的代理服务器,故需要加入这条配置
}
}
#LB-02二级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.7;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#此为可信任的代理服务器,故需要加入这条配置
}
}
#LB-03三级代理配置
server {
listen 80;
server_name ip.test.com;
location / {
proxy_pass http://10.0.0.8;
proxy_http_version 1.1;
Proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#此为可信任的代理服务器,故需要加入这条配置
}
}
#WEB主机配置,使用此模块是在后端WEB应用服务器添加参数
server {
listen 80;
server_name ip.test.com;
#定义可信任的代理服务器地址
set_real_ip_from 10.0.0.5;
set_real_ip_from 10.0.0.6;
set_real_ip_from 10.0.0.7;
#指定从哪个HTTP报头里检索IP信息
real_ip_header X-Forwarded-For;
#递归排除每个代理服务器的IP
real_ip_recursive on;
root /wwwroot;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
WEB主机测试脚本
#文件名:index.php
#放到测试目录/wwwroot内
<?php
$IP_ADDRESS = getenv("HTTP_X_FORWARD_FOR");
echo "SOURCE IP ADDRESS: $IP_ADDRESS";
?>
注:记得重载nginx
配置。
Wireshark抓包信息
HTTP报头信息
GET / HTTP/1.1
Host: ip.test.com
X-Forwarded-For: 10.0.0.1, 10.0.0.5, 10.0.0.6
Connection: close
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: UM_distinctid=16afc8d323f94-0360d7ebcc9bae-3d644509-18e414-16afc8d324063f
chrome打开测试
WEB应用服务器的访问LOG
結論:解決するためにX-Forwarded-For
カモフラージュ可能性IP
の問題を、私たちはバックエンド使用nginx_http_realip_module
のモジュールset_real_ip_from
の信頼性を追加するためのキーワードをIP
してからX-Forwarded-For
、クライアントの最終的な成功に取得するには、フィルタヘッドをIP
交換するために、ログフォーマットの一部を行うためにも、そしてクライアントがIP
で示されaccess.log
、ここで注意することは、最終的な値の分析を容易にするために、最初の列我々は正しいと絶対的に信頼できるプロキシサーバーを追加しなければならないということであるIP
そう最終結果はまだ問題があります。