ngx.req.get_headers()["X-Real-IP"] 是否能伪造客户端ip?

453 views
Skip to first unread message

kobeng

unread,
Jul 19, 2015, 6:48:58 AM7/19/15
to open...@googlegroups.com
我的源服务器前面有cdn , 日志记录的 $remote_addr 是cdn节点的ip, $http_x_forwarded_for 这个记录的真实的用户ip (用户ip未有伪造的情况下), 我想问问,源服务器前面有cdn的话,怎样才能获取真实的用户ip?

ps 如果客户端直接连接源服务器,   $remote_addr 这个能获取用户真实ip,用户ip比较难伪造。 

ngx.req.get_headers()["X-Real-IP"]  这个是不是还是从http header头获取用户ip?

wd

unread,
Jul 19, 2015, 10:00:20 AM7/19/15
to open...@googlegroups.com
http 头里面的信息是可以被中间服务器处理和产生的。
比如你和 cdn 那边商量好,他们把所有传给他们的 x-forwarded_for 头丢弃,然后设置这个头为 remote_addr,那就和你直接拿 remote_addr 没区别了。

只是举个例子。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

aaashun

unread,
Jul 26, 2015, 6:23:29 AM7/26/15
to openresty, w...@wdicc.com
如果你用的CDN只是充当个反向代理的角色的话, 像上边WD的方法是对的, 即和CDN供应商约定边缘节点通过特殊的header把客户端IP传给中心节点.

但是CDN都是在边缘节点缓存了资源, 应该绝大部分客户端请求是到不了中心节点的

牧童

unread,
Aug 15, 2015, 1:18:30 AM8/15/15
to openresty
ngx.req.get_headers()默认全小写吧

Jacky Zhang

unread,
Aug 15, 2015, 1:43:33 AM8/15/15
to open...@googlegroups.com
$cat /tmp/test.lua
ngx.say(ngx.req.get_headers()["X-Real-IP"])
ngx.say(ngx.req.get_headers()["REMOTE_ADDR"])
ngx.say(ngx.var.remote_addr)

----------------

$ curl http://localhost/lua/
nil
nil
127.0.0.1
----------------
$ curl http://localhost/lua/ -H'REMOTE-ADDR:1.2.3.4' -H'X-REAL-IP:1.2.3'
1.2.3
1.2.3.4
127.0.0.1
----------------
$ curl http://localhost/lua/ -H'REMOTE_ADDR:1.2.3.4' -H'X-REAL-IP:1.2.3'
1.2.3
nil
127.0.0.1

在 8/15/15 13:18, 牧童 写道:
> ngx.req.get_headers()默认全小写吧
>



Reply all
Reply to author
Forward
0 new messages