プロキシーの背後の 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.
The application setting trust proxy
may be set to one of the values listed in the following table.
型 | 値 |
---|---|
Boolean |
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.
|
trust proxy
を有効にすると、次の3つの重要な変更が起こります。
-
req.hostname の値は、クライアントまたはプロキシーが設定できる
X-Forwarded-Host
ヘッダーに設定された値から導き出されます。</li> -
X-Forwarded-Proto
は、https
とhttp
のどちらであるか、または無効な名前であるかをアプリケーションに通知するためにリバース・プロキシーによって設定できます。この値は、req.protocol に反映されます。 This value is reflected by req.protocol. -
req.ip および req.ips の値は、
X-Forwarded-For
のアドレス・リストから取り込まれます。</li> </ul>trust proxy
設定は、proxy-addr パッケージを使用して実装されます。詳細については、資料を参照してください。 For more information, see its documentation.