nginx 如何获取真实的 IP - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
testVmap
V2EX    问与答

nginx 如何获取真实的 IP

  testVmap 2018-09-18 13:47:05 +08:00 4613 次点击
这是一个创建于 2581 天前的主题,其中的信息可能已经有所发展或是发生改变。

客户端请求 A 服务器,A 服务器转发到 B B 的 nginx 日志是 A 的内网 IP,如何获得客户端的请求 IP

172.16.16.16 - - [09/Jun/2018:22:34:33 +0800] "GET /sogood/info.php HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"

第 1 条附言    2018-09-18 16:08:17 +08:00
获取到了真实 IP 加入黑名单似乎有问题
在 B 服务器添加了黑名单
使用 blacklist.conf,然后 nginx.conf 里面 include blacklist.conf.

问题:
直接访问 B 服务器,黑名单生效
通过访问 A 服务器转发到 B 服务器的请求似乎绕过了黑名单,能访问服务器,黑名单无法生效。
该怎样限制 IP
10 条回复    2018-09-18 15:51:35 +08:00
whatever93
    1
whatever93  
   2018-09-18 13:54:40 +08:00 via Android
x-forward-for
throns
    2
throns  
   2018-09-18 13:59:22 +08:00
可以看看这个配置:![Snipaste_2018-09-18_13-58-25.png]( https://i.loli.net/2018/09/18/5ba09414b3d96.png)
testVmap
    3
testVmap  
OP
   2018-09-18 14:02:57 +08:00
A 服务器转发配置
location ^~ /sogood {
proxy_pass http://172.16.0.7:81;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header Cookie $http_cookie;
proxy_set_header Referer $http_refere;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-For $remote_addr;
#proxy_set_header REMOTE-HOST $remote_addr;

}


B 服务器转发配置
location ~ \.php$ {
proxy_pass http://127.0.0.1;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header Cookie $http_cookie;
proxy_set_header Referer $http_refere;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}



加了感觉还是没用
lshero
    4
lshero  
   2018-09-18 14:06:41 +08:00
取 header 里的 X-Real-IP 啊
testVmap
    5
testVmap  
OP
   2018-09-18 14:10:38 +08:00
@lshero 是要怎么配置,要修改 log_format 吗
testVmap
    6
testVmap  
OP
   2018-09-18 14:11:10 +08:00
A 服务器的日志能正常获取到真实 IP
throns
    7
throns  
   2018-09-18 14:12:39 +08:00 via Android   1
lshero
    8
lshero  
   2018-09-18 14:51:07 +08:00   1
@testVmap 如果是从日志获取的话。log_format 可以参考一下这个 http://blog.51cto.com/nicking/1431056。如果程序要取的话把所有的 header 里添加的 X-Forwarded-For 或者 X-Real-IP 都成
jtsai
    9
jtsai  
   2018-09-18 15:22:09 +08:00   1
A 服务把客户端的请求头转到 B 服务器

location / {
# HTTP header
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass (B 服务器地址)

}
testVmap
    10
testVmap  
OP
   2018-09-18 15:51:35 +08:00
感谢大家,最后配置如下
服务器 A 的转发配置
location ^~ /namesg/ {
proxy_pass http://172.16.16.11;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}

服务器 B 的 nginx.conf log 配置

log_format my_format '$http_x_real_ip -'
'$remote_addr- [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log my_format;


主要是 nginx 日志取消注释后还不能生效,需要改下日志名字
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2810 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 24ms UTC 13:31 PVG 21:31 LAX 06:31 JFK 09:31
Do have faith in what you're doing.
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86