代理背后的 Express
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.
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.
在代理背后运行 Express 应用程序时,可使用 app.set() 将应用程序变量 trust proxy
设置为下表中所列的某个值。
类型 | 值 |
---|---|
布尔 |
如果为 If When setting to |
IP addresses |
An IP address, subnet, or an array of IP addresses and subnets to trust as being a reverse proxy. The following list shows the pre-configured subnet names:
您可以按以下某种方法设置 IP 地址:
如果指定 IP 地址或子网,那么会在地址确定过程中排除这些项,而将最接近应用程序服务器的不受信任的 IP 地址确定为客户机的 IP 地址。 This works by checking if |
数字 |
Use the address that is at most 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. |
Function |
Custom trust implementation.
|
Enabling trust proxy
will have the following impact:
-
req.hostname 的值派生自
X-Forwarded-Host
头中设置的值(可以由客户机或代理设置此值)。 -
X-Forwarded-Proto
可以由逆向代理设置,以告知应用程序:它是https
还是http
,或者甚至是无效名称。该值由 req.protocol 反映。 This value is reflected by req.protocol.
trust proxy
设置由使用 proxy-addr 包实现。有关更多信息,请参阅其文档。 For more information, see its documentation.