🌐 Nodejs.cn

通过代理表达

在反向代理后运行 Express 应用时,一些 Express API 可能返回的值与预期不同。为了调整这一点,可以使用 trust proxy 应用设置在 Express API 中显示由反向代理提供的信息。最常见的问题是,显示客户端 IP 地址的 Express API 可能会显示反向代理的内部 IP 地址。

🌐 When running an Express app behind a reverse proxy, some of the Express APIs may return different values than expected. In order to adjust for this, the trust proxy application setting may be used to expose information provided by the reverse proxy in the Express APIs. The most common issue is express APIs that expose the client’s IP address may instead show an internal IP address of the reverse proxy.

Note

在配置 trust proxy 设置时,了解反向代理的具体设置非常重要。由于此设置会信任请求中提供的值,因此确保 Express 中的设置与反向代理的运行方式相匹配非常重要。

🌐 When configuring the trust proxy setting, it is important to understand the exact setup of the reverse proxy. Since this setting will trust values provided in the request, it is important that the combination of the setting in Express matches how the reverse proxy operates.

应用设置 trust proxy 可以设置为下表中列出的值之一。

🌐 The application setting trust proxy may be set to one of the values listed in the following table.

类型
布尔值

如果 true,客户端的 IP 地址被理解为 X-Forwarded-For 头中的最左边条目。

如果 false,该应用被理解为直接面向客户端,并且客户端的 IP 地址由 req.socket.remoteAddress 得出。这是默认设置。

🌐 If false, the app is understood as directly facing the client and the client’s IP address is derived from req.socket.remoteAddress. This is the default setting.

Warning

当设置为 true 时,确保最后一个受信任的反向代理正在移除/覆盖以下所有 HTTP 头:X-Forwarded-ForX-Forwarded-HostX-Forwarded-Proto 非常重要,否则客户端可能提供任意值。

🌐 When setting to true, it is important to ensure that the last reverse proxy trusted is removing/overwriting all of the following HTTP headers: X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto, otherwise it may be possible for the client to provide any value.

IP 地址

要信任为反向代理的 IP 地址、子网或 IP 地址和子网数组。以下列表显示了预配置的子网名称:

  • 回环 - 127.0.0.1/8::1/128
  • 链路本地 - 169.254.0.0/16fe80::/10
  • uniquelocal - 10.0.0.0/8172.16.0.0/12192.168.0.0/16fc00::/7

你可以通过以下任意一种方式设置IP地址:

🌐 You can set IP addresses in any of the following ways:

app.set('trust proxy', 'loopback'); // specify a single subnet
app.set('trust proxy', 'loopback, 123.123.123.123'); // specify a subnet and an address
app.set('trust proxy', 'loopback, linklocal, uniquelocal'); // specify multiple subnets as CSV
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']); // specify multiple subnets as an array

当指定时,IP 地址或子网会从地址确定过程中排除,并且离应用服务器最近的不受信任的 IP 地址会被确定为客户端的 IP 地址。这是通过检查 req.socket.remoteAddress 是否被信任来工作的。如果是,那么会从右到左检查 X-Forwarded-For 中的每个地址,直到找到第一个不受信任的地址。

🌐 When specified, the IP addresses or the subnets are excluded from the address determination process, and the untrusted IP address nearest to the application server is determined as the client’s IP address. This works by checking if req.socket.remoteAddress is trusted. If so, then each address in X-Forwarded-For is checked from right to left until the first non-trusted address.

编号

使用距离 Express 应用最多为 n 跳的地址。 req.socket.remoteAddress 是第一跳,其余的地址从右到左在 X-Forwarded-For 头中查找。值为 0 意味着第一个不可信的地址将是 req.socket.remoteAddress,即没有反向代理。

Warning

使用此设置时,重要的是要确保不存在多个不同长度的路径通向 Express 应用,以免客户端距离配置的跳数少于预期,否则客户端可能能够提供任意值。

🌐 When using this setting, it is important to ensure there are not multiple, different-length paths to the Express application such that the client can be less than the configured number of hops away, otherwise it may be possible for the client to provide any value.

功能

自定义信任实现。

app.set('trust proxy', (ip) => {
if (ip === '127.0.0.1' || ip === '123.123.123.123')
return true; // trusted IPs
else return false;
});

启用 trust proxy 将产生以下影响:

🌐 Enabling trust proxy will have the following impact:

  • req.hostname 的值来源于 X-Forwarded-Host 头中设置的值,该值可以由客户端或代理设置。

  • X-Forwarded-Proto 可以由反向代理设置,用于告诉应用它是 https 还是 http,甚至是一个无效的名称。此值通过 req.protocol 反映出来。

  • req.ipreq.ips 的值是根据套接字地址和 X-Forwarded-For 头生成的,从第一个不受信任的地址开始。

trust proxy 设置是使用 proxy-addr 包实现的。有关更多信息,请参阅其文档。

🌐 The trust proxy setting is implemented using the proxy-addr package. For more information, see its documentation.