joe*_*zza 42 proxy nginx node.js express
我正在写一个位于nginx服务器后面的快速应用程序.我正在阅读express的文档,并提到了'trust proxy'设置.它只是说
信任代理启用反向代理支持,默认情况下禁用
我在这里阅读了一篇用nginx解释Node中的安全会话的小文章.
http://blog.nikmartin.com/2013/07/secure-sessions-in-nodejs-with-nginx.html
所以我很好奇.将'trust proxy'设置为true只会在使用HTTPS时生效吗?目前我的应用程序只是客户端和nginx之间的HTTP.如果我现在将其设置为true,是否需要注意任何副作用/影响?现在有什么意义吗?
Aks*_*rma 39
这在代理指南背后的表达中有详细解释
通过app.enable('trust proxy')启用"信任代理"设置,Express将知道它位于代理后面并且X-Forwarded-*头字段可能是可信的,否则可能很容易被欺骗.
启用此设置有几个微妙的影响.第一个是X-Forwarded-Proto可以由反向代理设置,告诉应用程序它是https或只是http.该值由req.protocol反映.
第二个更改是req.ip和req.ips值将填充X-Forwarded-For的地址列表.
Kav*_*uwa 19
TLDR:应用程序设置信任代理只是为了在快速应用程序位于代理后面时使用。当有代理时启用此功能有助于通过众所周知的标头(主要是 X-Forwarded-For、X-Forwarded-Proto)解析以下属性
更多细节
当我搜索信任代理对于Express-Session 的真正作用时,我最终来到这里。没有一个答案对我有帮助。
默认值 - false(禁用)
IMO 最好的文档位于应用程序设置
信任代理
指示应用程序位于前置代理后面,并使用 X-Forwarded-* 标头来确定客户端的连接和 IP 地址。注意:X-Forwarded-* 标头很容易被欺骗,并且检测到的 IP 地址不可靠。
启用后,Express 会尝试确定通过前端代理或一系列代理连接的客户端的 IP 地址。该
req.ips属性包含客户端连接所通过的 IP 地址数组。要启用它,请使用信任代理选项表中描述的值。该
trust proxy设置是使用 proxy-addr 包实现的。有关更多信息,请参阅其文档。注意:子应用程序将继承此设置的值,即使它有默认值。
ps - 如果您想了解这对快速会话有何帮助,则需要启用信任代理才能获取req.secure的正确值
ann*_*neb 15
用于解释信任代理使用的带注释的代码
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27139 次 |
| 最近记录: |