Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express中的trust proxy设置 #3

Open
xizhibei opened this issue Apr 15, 2016 · 0 comments
Labels

Comments

@xizhibei
Copy link
Owner

@xizhibei xizhibei commented Apr 15, 2016

一般来说,我们的项目都是放在反向代理后面的,比如nginx,haproxy之类的,这时候就会有个问题,你获取的IP地址可能一直是前面代理的IP,而不是用户端的IP,于是这时候express就需要设置下trust proxy了

默认是false,也就是不信任任何代理,比如你所在的私有网络是 10.0.0.0/8,那么直接设置:

app.set('trust proxy', '10.0.0.0/8');

即可,然后在代码中获取 req.ip 就可获得客户端的地址,req.ips 里面放是从 x-forwarded-for
取出的地址,当然了,地址会经过过滤处理,只会根据你设置的规则,只会把根据设置的规制,把受信任的IP地址留下。

然后,如果你觉得某个代理IP是值得信任的,也可以单独设置:

app.set('trust proxy', '10.0.0.0/8', '123.123.123.123');

当然,特殊的规制还可以用函数:

app.set('trust proxy', ip =>  /^123/.test(ip));

具体请参考官方文档

@xizhibei xizhibei added the Node.js label Apr 20, 2016
@xizhibei xizhibei referenced this issue Oct 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.