Express hinter Proxys
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.
Bei der Ausführung einer Express-Anwendung hinter einem Proxy legen Sie die Anwendungsvariable trust proxy
(mithilfe von app.set()) auf einen der in der folgenden Tabelle enthaltenen Werte fest:
Typ | Wert |
---|---|
Boolesch |
Wenn Wenn 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:
Sie können IP-Adressen wie folgt festlegen:
Sobald die Werte angegeben wurden, werden die betreffenden IP-Adressen und Teilnetze aus dem Adressfeststellungsprozess ausgeschlossen. Die nicht vertrauenswürdige IP-Adresse, die am nächsten zum Anwendungsserver liegt, wird als IP-Adresse des Clients festgelegt. This works by checking if |
Zahl |
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:
-
Der Wert für req.hostname wird vom Wert abgeleitet, der im Header
X-Forwarded-Host
festgelegt wurde. Dieser Wert kann vom Client oder Proxy festgelegt werden.</li> -
X-Forwarded-Proto
kann vom Reverse Proxy festgelegt werden, um der Anwendung mitzuteilen, ob es sich umhttps
oderhttp
oder sogar um einen ungültigen Namen handelt. Dieser Wert wird durch req.protocol abgebildet. -
Als Werte für req.ip und req.ips wird die Liste der Adressen aus
X-Forwarded-For
herangezogen.
</ul>
Die Einstellung für trust proxy
wird mithilfe des proxy-addr-Pakets implementiert. Weitere Informationen finden Sie in der zugehörigen Dokumentation.