代理背后的 Express

在反向代理后面运行 Express 应用程序时,某些 Express API 可能返回与预期不同的值。为了对此进行调整,trust proxy 应用程序设置可用于公开由 Express API 中的反向代理提供的信息。最常见的问题是公开客户端 IP 地址的快速 API 可能会显示反向代理的内部 IP 地址。

配置 trust proxy 设置时,了解反向代理的确切设置非常重要。由于此设置将信任请求中提供的值,因此 Express 中的设置组合与反向代理的操作方式相匹配非常重要。

应用程序设置 trust proxy 可以设置为下表中列出的值之一。

类型

布尔值

如果是 true,则客户端的 IP 地址被理解为 X-Forwarded-For 标头中最左边的条目。

VmqBX2CbLR/lHY5vCjpWLUt8APxu/FNKhziyTNeo9gtDckQMggp3Gx+LQnrnCXaLbC+dWL00Uln47U3Rr4Q9EC9TPVfblAE3ecuejBLiyk2seisNiZI0qSagTDKcjIcPzLObSoV9KlzLPX/fHIXiIPsm/jfzLim37dNbCyBHaT0hkOSCoKiAE9rFyf/1+f9pCdwoj72pun2prrLc4b++JFfPIB7msFVy2ukA5zdz/+c=

Q+qj1tBnHa1Xo1w1N3UW7jQa7eSe5ynxn8wKKPIvuob3TPM4RUsl0+qUlGlUjbbgCE0So3ZUtdEEkOllUWfITEuIwfDRyzOtGg9YG+7AUi8SwwbjWCQIzke1kaPzxwVs1zo+YpsIvQx+K5Hp6/lc3AB1CCUb8Uo/AZx/dGHtT48hruT24sRhfufP5XBoJUaNiRQgk1r1bpS/F8KKJSXQqUF4d05WUd4Q2frJB7+6UowCQaxOHLYVgx6GZgyNE/JC2BXcSVAGC7BCYhAeVxhFQ/+rZaUgpfGVBcDSQRkV1lwNAOW2GkCBvJN5R1E/DVrcoyFCUQKygO6Pk4UgnZmzCimkCxDyV1Thqsei5bLKJw4=

Hswnw+eJ0MXbqy6HqeJ3bg==

uIA75P8FpyHvd8B6o1uDVl0SA3jLIuO8WUcJ3u/CrNtibC+mp3Ua1JceaK+Kt1zRYDNN5dDIEZ0Dz1REmVzqOK5hkaBiPKuCr1j8d5HQxKpypxoUXIak/fY3VxR8R9CK2HF7pJ+KjE70PTISfeTXKKcoUPWWQAkm1LpH5E7N3jXo7AVndLpfRPzUyN63MBFl

    hKqc1Xjhi1CPTkKFHhFOM9HobwQc37YFb0TAyRWqflyLyLaekNq3gRgmSrYcgR97/t23c+5t8/5KLGP6O+596Zt4W4jdQgC4Dm7Vsf06/nQdPlsOHeLuETHnQy9qlcNJGt5q7Wl7j524z6jAHdbtTiYR26A34sEuJnqcuBMaEZqLTIQkCSTykOlf6A0aT3zGRpLwKctTeo+Fri/1MbRPriJYlaYFNOwrG6Fayuf/YCZPSDBES3XWLfv5DgZbXDri4ufA11/j+gbI3TmETaOO6+6PWD6amNyrq1rIup2WuoDZlHozrl+DIX9l/J9Kqbdn49DxKca2JxRHK1lQTkivEHNEnq5txt/eDBGOsjIV+OXZXqGFma+xe6qdieUZKAQG

dCJrVLICKUtOZiyvhjU6EyZl2LMrjZ8r4WzJ4plMpUo3wVxUOkA/PZ/5Ky5uFqtxtl2hRKfH6ZumlemBjn/J9Q==

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

h5HTfO9lDYM5mCMGuhhVIGnz6yBGRnelXjCwRmFEuWkRRnLKF7XrhBAJHFFlaUGVM8exz7ugFU/uWF+Xd4pquILFsu0WVm1ple5/wCkdW1cVxCTiDPmTfzympY3KoLql/3YXq1n4vlNiD+kiZ81J8tUTO/Vy9DqRTJhO7VQxr1z862t4BqsBMZm1VsIOKSan2I5Yn5VtbHBT8llBco3gWr9D1R57Xn7HPlGOHfbEMCuQzn3ETFXsoNi20CaTo4qaIR3Pov/ef6VHoin7jxXgxfw1zJwAnZeryvu3r156rn3x7yq7r8CsxOkMx6li2m8rF3Xsq84wra2Vf2NNbQqRwSjl8PpJsvCKifjetPJ8p62XEE/7QsbhxIkQauQGza3RIJ5h3GAm1goK5ke4JIlAYegXp1pL6mn6FcQcVzRlspaSc8M13dJWkwTVa4JgqVFQSv9JMbgB2njVmLdNM7jRMv9UNuT2mSJEj3+MWLmyFwk8xUSGbKJECIK+syxeonYp

h7FApsrzRpsdwEY89RuobA==

Wp06LtDWtlOJWxhTPSL3h4DXDU0FIPRbgwmlPK2AuUDwaOdkeLLXFcxufZunJDoIKFthQGErG0OkrcwBUkX7Ag20jCJSPSPF0wSKEhl+6cu+uxba1E1gSEfCDRhGjruh0/c6kyEiPKvTOfGW8y3oBfkAwfYmXmlxLcE3cmC1IcxAocu0W3rLEHxR8YX0ZsN6QymHW4o5dgQ5MKwH/pl72qpdiJd+VPp2I1rltkIJ8W0weu9I2yre0EDxYxmnI+RFNJBVnhUHXRS2iA5fy6rH0zcWnhGx2VCyxEKp2MSoOthvDS7qb7Drcninmm0T29aevkBLdJss1iWl1D01CNrJNEbg1PuWbHP85fZwt/ssgAgW6VBftP7+mCoZJEGL0zs5E3lqpFB7T9Pvl7NJdKLbUCUtnQSJyMx9hFLSEP6rS9eKWjgbdK1pqsxwWIUPy25p

ip3r0HGLSUOw9oAGHNDLVsBvvBwu2xoYqq8MCbhvMUDvXM5x/7CuZLCyr8lwrrKwouAaSXAZwTx9npw8Ipe42Y/zygZzcbZ7Y519kVzxIoS67ggwaiNIjrV9cp8Z/30VIjZj+D0ZDgCzH+iNycXT/CvZtc7nfrZI1V7fO8ZKVVnTyyzl6eFNZG1KO3hXSG1dIwp3USM8JK6vC1EgaoqlAs/gmvAu9DWmROis3rklQnNEjA6L4CFOr0oxg559BONu

nfoHk4FEJCz2dU0j+GE3+g==

DPGLPPQD+OVSEjPl9Rz/hrtGaCLv9ohXFN135v9cQQg=

app.set('trust proxy', (ip) => {
  if (ip === '127.0.0.1' || ip === '123.123.123.123') return true // trusted IPs
  else return false
})

/PNKI+tnhwrg90VhJdisEF9FdU+goIT+TtzewkVP61bbLTnGFZuguXfNqd0sLEXYX4kYiuqmUJS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