Express atrá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.
Ao executar um aplicativo do Express atrás de um proxy,
configure (usando app.set()) a variável do
aplicativo trust proxy
para um dos valores
listados na seguinte tabela.
Tipo | Valor |
---|---|
Booleano |
Se Se 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:
É possível configurar endereços de IP de qualquer uma das formas a seguir:
Quando especificados, os endereços de IP ou sub-redes são
excluídos do processo de determinação de endereço, e o endereço de
IP não confiável mais próximos do servidor de aplicativos é
determinado como o endereço de IP do cliente. This works by checking if |
Número |
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:
-
O valor de req.hostname é derivado do valor configurado no cabeçalho
X-Forwarded-Host
, que pode ser configurado pelo cliente ou pelo proxy. -
X-Forwarded-Proto
pode ser configurado pelo proxy reverso para dizer ao aplicativo se ele éhttps
ouhttp
ou até um nome inválido. Este valor é refletido pelo req.protocol. -
Os valores req.ip e req.ips são populados com a lista de endereços do
X-Forwarded-For
.
A configuração do trust proxy
é
implementada usando o pacote
proxy-addr. Para
obter mais informações, consulte a documentação.