简体   繁体   English

Apache:如何使用负载均衡器和2台服务器编写X-Forwarded-For?

[英]Apache: How to write X-Forwarded-For with load balancer and 2 servers?

I have a load balancer and two servers. 我有一个负载平衡器和两个服务器。 I want to add X-Forwarded-For in my apache so that I can see the IP of the request as opposed to seeing the load balancer IP for every request. 我想在我的apache中添加X-Forwarded-For,这样我就可以看到请求的IP,而不是看到每个请求的负载均衡器IP。 How would I do this? 我该怎么做? I looked it up and X-Forwarded-For: client, proxy1, proxy2 looks like a solution. X-Forwarded-For: client, proxy1, proxy2了一下, X-Forwarded-For: client, proxy1, proxy2看起来像一个解决方案。 Would client be the load balancer IP, proxy1 be Server 1's IP, and proxy2 be Server 2's IP? 客户端将是负载均衡器IP,proxy1是服务器1的IP,proxy2是服务器2的IP吗?

I assume you already referenced http://en.wikipedia.org/wiki/X-Forwarded-For - which is likely where you copied your X-Forwarded-For: client, proxy1, proxy2 format from. 我假设您已经引用了http://en.wikipedia.org/wiki/X-Forwarded-For-这很可能是您从中复制X-Forwarded-For: client, proxy1, proxy2格式。

I also assume you're using a dedicated load balancer (something other than Apache HTTPD), which directs traffic to your "servers" - with your "Apache" included on each server, and that you know how to configure your load balancer to pass this header. 我还假设您正在使用专用的负载均衡器(Apache HTTPD之外的其他工具),它将流量定向到“服务器”-每个服务器上都包含“ Apache”,并且您知道如何配置负载均衡器以使其通过此标头。

I think the missing piece that you need to understand is this (also from the Wikipedia page): 我认为您需要了解的缺失部分是这样的(也来自Wikipedia页面):

where the value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. 其中的值是逗号分隔的IP地址列表,最左边的是原始客户端,每个通过请求的后续代理都将接收请求的IP地址添加到该IP地址中。 In this example, the request passed through proxy1, proxy2, and then proxy3 (not shown in the header). 在此示例中,请求先通过proxy1,proxy2,再通过proxy3(标头中未显示)传递。 proxy3 appears as remote address of the request. proxy3显示为请求的远程地址。

IE, if your load balancer is the only proxy that a given request goes through (at least, the only one that adds a X-Forwarded-For header), then the header will only include one value - that of your load balancer. IE,如果您的负载均衡器是给定请求通过的唯一代理(至少是添加X-Forwarded-For标头的唯一代理),则标头将仅包含一个值-您的负载均衡器的值。

Assume you are handling a request from a client with 203.0.113.1, and that request comes through your load balancer that has an IP of of 192.0.2.1. 假设您正在处理来自203.0.113.1的客户端的请求,并且该请求来自IP为192.0.2.1的负载均衡器。 Your Apache HTTP instance running on one of your 2 servers should then see the request as coming from 192.0.2.1, with: X-Forwarded-For: 203.0.113.1 . 然后,在两台服务器之一上运行的Apache HTTP实例应该看到来自192.0.2.1的请求,并带有: X-Forwarded-For: 203.0.113.1

In order to see this header in your Apache HTTPD logs, you need to use something like this: 为了在Apache HTTPD日志中看到此标头,您需要使用以下代码:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D \"%{Host}i\" \"%{X-Forwarded-For}i\"" custom

The load balancer's IP (192.0.2.1) will still show as the 1st field ( %h ) in the log entry, but the last field ( \\"%{X-Forwarded-For}i\\" ) will contain the client's IP(s) - in this case, 203.0.113.1 . 负载平衡器的IP(192.0.2.1)仍将在日志条目中显示为第一个字段( %h ),但最后一个字段( \\"%{X-Forwarded-For}i\\" )将包含客户端的IP( s)-在这种情况下为203.0.113.1 If the client is behind additional proxies that also report X-Forwarded-For , then will be a comma-separated list - with the left-most address being the "closest" to your servers - IE, the one that your load balancer received the request from. 如果客户端位于同时报告X-Forwarded-For其他代理之后,则将是一个逗号分隔的列表-最左边的地址是离服务器“最近”的地址IE,即负载均衡器收到的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM