en/guide/behind-proxies.md
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.
The application setting trust proxy may be set to one of the values listed in the following table.
If false, the app is understood as directly facing the client and the client's IP address is derived from req.socket.remoteAddress. This is the default setting.
127.0.0.1/8, ::1/128169.254.0.0/16, fe80::/1010.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7You can set IP addresses in any of the following ways:
app.set('trust proxy', 'loopback') // specify a single subnet
app.set('trust proxy', 'loopback, 123.123.123.123') // specify a subnet and an address
app.set('trust proxy', 'loopback, linklocal, uniquelocal') // specify multiple subnets as CSV
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']) // specify multiple subnets as an array
When specified, the IP addresses or the subnets are excluded from the address determination process, and the untrusted IP address nearest to the application server is determined as the client's IP address. This works by checking if req.socket.remoteAddress is trusted. If so, then each address in X-Forwarded-For is checked from right to left until the first non-trusted address.
app.set('trust proxy', (ip) => {
if (ip === '127.0.0.1' || ip === '123.123.123.123') return true // trusted IPs
else return false
})
Enabling trust proxy will have the following impact:
The trust proxy setting is implemented using the proxy-addr package. For more information, see its documentation.