Express detrás de proxies
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.
Type | Value |
---|---|
Booleano |
Si es Si es 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:
Puede establecer direcciones IP de varias formas:
Cuando se especifican, las direcciones IP o las subredes se excluyen del proceso de determinación de direcciones, y la dirección IP no de confianza más próxima al servidor de aplicaciones se establece como la dirección IP del cliente. This works by checking if |
Number |
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:
-
El valor de req.hostname se obtiene del valor definido en la cabecera
X-Forwarded-Host
, que puede estar establecido por el cliente o el proxy. -
El proxy inverso puede establecer
X-Forwarded-Proto
para indicar a la aplicación si eshttps
,http
o incluso un nombre no válido. req.protocol refleja este valor. -
Los valores req.ip y req.ips se rellenan con la lista de direcciones de
X-Forwarded-For
.
El valor trust proxy
se implementa utilizando el paquete proxy-addr. For more information, see its documentation.