我正在编写一个位于nginx服务器后面的快速应用程序。我正在阅读express的文档,并提到了'trust proxy'设置。它只是说
trust proxy启用反向代理支持,默认情况下禁用
我在这里阅读了一篇用nginx解释Node中的安全会话的小文章。
http://blog.nikmartin.com/2013/07/secure-sessions-in-nodejs-with-nginx.html
所以我很好奇。将'trust proxy'设置为true只会在使用HTTPS时生效吗?目前我的应用程序只是客户端和nginx之间的HTTP。如果我现在将其设置为true,是否需要注意任何副作用/影响?是否有任何意义将其设置为现在?
答案 0 :(得分:37)
express behind the proxies guide
详细说明了这一点启用"信任代理"通过app.enable设置('信任代理'),Express会知道它位于代理后面,并且X-Forwarded- *标题字段可能是可信的,否则可能很容易欺骗。
启用此设置会产生一些微妙的影响。第一个是X-Forwarded-Proto可以由反向代理设置,告诉应用程序它是https或只是http。该值由req.protocol反映。
第二个更改是req.ip和req.ips值将填充X-Forwarded-For的地址列表。
答案 1 :(得分:11)
解释使用信任代理的注释代码
var express = require('express');
var app = express();
// Set the ip-address of your trusted reverse proxy server such as
// haproxy or Apache mod proxy or nginx configured as proxy or others.
// The proxy server should insert the ip address of the remote client
// through request header 'X-Forwarded-For' as
// 'X-Forwarded-For: some.client.ip.address'
// Insertion of the forward header is an option on most proxy software
app.set('trust proxy', '127.0.0.1');
app.get('/test', function(req, res){
var ip = req.ip; // trust proxy sets ip to the remote client (not to the ip of the last reverse proxy server)
if (ip.substr(0,7) == '::ffff:') { // fix for if you have both ipv4 and ipv6
ip = ip.substr(7);
}
// req.ip and req.protocol are now set to ip and protocol of the client, not the ip and protocol of the reverse proxy server
// req.headers['x-forwarded-for'] is not changed
// req.headers['x-forwarded-for'] contains more than 1 forwarder when
// there are more forwarders between the client and nodejs.
// Forwarders can also be spoofed by the client, but
// app.set('trust proxy') selects the correct client ip from the list
// if the nodejs server is called directly, bypassing the trusted proxies,
// then 'trust proxy' ignores x-forwarded-for headers and
// sets req.ip to the remote client ip address
res.json({"ip": ip, "protocol": req.protocol, "headers": req.headers['x-forwarded-for']});
});
// in this example the reverse proxy is expected to forward to port 3110
var port = 3110;
app.listen(port);
// test through proxy: http://yourproxyserver/test, req.ip should be your client ip
// test direct connection: http://yournodeserver:3110/test, req.ip should be your client ip even if you insert bogus x-forwarded-for request headers
console.log('Listening at http://localhost:' + port);
答案 2 :(得分:0)
TLDR :应用程序设置 信任代理 仅用于快速应用程序位于代理之后的情况。在有代理时启用此功能有助于通过众所周知的标头(主要是 X-Forwarded-For、X-Forwarded-Proto)解析以下属性
更多详情
我在搜索 信任代理 对 express-session 的真正作用时,最终来到了这里。没有一个答案对我有帮助。
默认值 - false(禁用)
IMO 最好的文档位于 Application Settings
<块引用>信任代理
表示该应用位于前置代理之后,并使用 X-Forwarded-* 标头来确定连接和 IP 地址 客户的。注意:X-Forwarded-* 标头很容易被欺骗,并且 检测到的 IP 地址不可靠。
启用后,Express 会尝试确定
客户端通过前置代理或一系列代理连接。
req.ips
属性,然后包含 IP 地址数组
客户端是通过连接的。要启用它,请使用中描述的值
信任代理选项表。
trust proxy
设置是使用 proxy-addr 包实现的。
如需了解详情,请参阅其文档。
注意:子应用程序将继承此设置的值,即使它 有一个默认值。
p.s - 如果您想了解这对 express-session 有何帮助,则需要启用信任代理才能获得 req.secure
的正确值