Express con i proxy
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.
Quando si esegue un’applicazione Express con un proxy, impostare (utilizzando app.set()) la variabile dell’applicazione trust proxy
su uno dei valori elencati nella seguente tabella.
Tipo | Valore |
---|---|
Booleano |
Se impostato su Se impostato su 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:
È possibile impostare gli indirizzi IP in uno dei seguenti modi:
Quando specificati, gli indirizzi IP o le subnet vengono esclusi dal processo di determinazione dell’indirizzo e l’indirizzo IP non attendibile più vicino al server delle applicazioni viene considerato come indirizzo IP del client. This works by checking if |
Numero |
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:
-
Il valore di req.hostname viene rilevato dalla serie di valori nell’intestazione
X-Forwarded-Host
, la quale può essere impostata dal client o dal proxy. -
X-Forwarded-Proto
può essere impostata dal proxy inverso per far capire all’applicazione se si tratta dihttps
ohttp
oppure di un nome non valido. Questo valore viene riportato da req.protocol. -
I valori req.ip e req.ips vengono popolati con l’elenco di indirizzi da
X-Forwarded-For
.
L’impostazione trust proxy
viene implementata utilizzando il pacchetto proxy-addr. Per ulteriori informazioni, consultare la relativa documentazione.