从 Apache 负载均衡器重定向的请求中获取原始远程主机

Get original remote host from the request redirected by the Apache load balancer

我有 2 个 Apache Http 服务器 2.4 作为我的应用程序的负载平衡器。我 运行 遇到的问题是我的应用程序选择远程主机作为负载均衡器之一,但我实际上想要请求的真正来源,就像在发起调用的客户端中一样。 有人知道如何实际获得真正的远程主机吗?

来自 Apache 2.4 mod_proxy documentation:

When acting in a reverse-proxy mode (using the ProxyPass directive, for example), mod_proxy_http adds several request headers in order to pass information to the origin server. These headers are:

  • X-Forwarded-For The IP address of the client.

  • X-Forwarded-Host The original host requested by the client in the Host HTTP request header.

  • X-Forwarded-Server The hostname of the proxy server.

Be careful when using these headers on the origin server, since they will contain more than one (comma-separated) value if the original request already contained one of these headers. For example, you can use %{X-Forwarded-For}i in the log format string of the origin server to log the original clients IP address, but you may get more than one address if the request passes through several proxies.

所以您实际上需要从您的最终应用程序中捕获 X-Forwarded-For HTTP header。但要小心,正如文档所说,它也可能包含 Apache 代理前面的所有中间代理的 IP 地址。