haproxy并将客户端IP地址转发给服务器

我有一个HAproxy的问题。

我使用HAproxy作为负载平衡器,将传入的http请求分配给5个Web服务器。 通常客户端请求会通过负载均衡器的IP转发到Web服务器。 但是我需要客户IP或真正的IP从Web服务器请求一些东西。 因为我们需要logging真实客户端的IP。

我尝试在Web服务器上获取客户端的IP,但是我现在还不能成功。 总是看到负载均衡器的IP。

我使用x-forward-for选项,但没有解决问题。 之后,我发现另一个选项“ 源0.0.0.0:80 usesrc clientip ”,但我试图运行HAproxy这是关于编译需要与HAproxy的USE_TPROXY选项的eror。 我做到了,我用USE_TPROXY选项重新编译HAproxy,但不会改变任何东西。 我能做些什么来学习真实的客户IP。

我的linux内核版本是2.6.32-34我的意思是内核支持透明代理。 我使用UBUNTU 10.4 LTS

我的configuration文件在这里

global maxconn 100000 uid 99 gid 99 daemon defaults option forwardfor except 127.0.0.1 mode http (1)source 0.0.0.0:80 interface hdr_ip(x-forwarded-for,-1) (2)source 0.0.0.0:80 usesrc clientip contimeout 5000 clitimeout 50000 srvtimeout 50000 listen myWeb 0.0.0.0:80 mode http balance source option forwardfor header X-Client option http-server-close stats enable stats refresh 10s stats hide-version stats scope . stats uri /lb?stats stats realm LB2\ Statistics stats auth admin:xXx server S1 192.168.1.117:80 check inter 2000 fall 3 server S2 192.168.1.116:80 check inter 2000 fall 3 server S3 192.168.1.118:80 check inter 2000 fall 3 

(1)(2)在testingHAproxy时,我使用了这两行之一。

有人帮助我学习从我们的服务器请求的客户的真正的IP?

我解决了这个问题。 可能从一开始就不是问题。 当我遇到这个问题时,我做了谷歌search,我看到了

 option forwardfor 

以便在haproxy.cfg文件中使用以及其他选项。 我尝试了这些选项,包括重新编译haproxy …但是,在Web服务器上学习真实客户端IP的真正问题不是来自HAproxy,而是由服务器脚本读取头文件,在我们的例子中,这种脚本语言是PHP。

我尝试通过这些命令学习客户端的IP

 echo 'Client IP: '.$_SERVER["REMOTE_ADDR"]; echo 'Client IP: '.$_SERVER["HTTP_CLIENT_IP"]; 

这些命令显示负载均衡器的IP。 这是正确的,但这不是我所期望的。 尽pipeforwardfor选项这些命令,给了我loadbalancer的IP

通过使用forwardfor选项,我们使enable HAproxy能够将x-forwarded-for头插入发送到我们Web服务器的客户端请求中。 HAproxy把这个领域的标题,但我忽略了这一点。 今天,我意识到这是一个标题字段,我必须这样读这个标题

 echo 'Client IP: '.$_SERVER["HTTP_X_FORWARDED_FOR"]; 

通过这个命令,我得到了客户端的IP地址,而不是负载均衡器的IP地址。

但是我的提议是为了获得头文件数据来调查其他信息是PHP的getallheaders()函数。

 //from php.net http://php.net/manual/en/function.getallheaders.php foreach (getallheaders() as $name => $value) { echo "$name: $value<br>\n"; } 

所有我最后的haproxy.cfg文件结束如下所示。

 global maxconn 100000 uid 99 gid 99 daemon defaults option forwardfor except 127.0.0.1 mode http contimeout 5000 clitimeout 50000 srvtimeout 50000 listen myWeb 0.0.0.0:80 mode http balance source option forwardfor option http-server-close stats enable stats refresh 10s stats hide-version stats scope . stats uri /lb?stats stats realm LB2\ Statistics stats auth admin:passwd server S1 192.168.1.117:80 check inter 2000 fall 3 server S2 192.168.1.116:80 check inter 2000 fall 3 server S3 192.168.1.118:80 check inter 2000 fall 3 

不过,我有很多关于HAproxy的东西,比如uid或者gid是什么意思。

如果您需要Apache日志中的客户端IP地址,您可以更改您的apache conf来loggingX-forwarded-for的原始源代码(5h)

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

只是试过HTTP_X_FORWARDED_FOR的解决scheme,似乎头名称已从HTTP_X_FORWARDED_FOR更改为x-forwarded-for 。 可能与HAproxy版本有关,因为答案是5年前写的…?

作为一个例子,这正在生产:

String requestIp = httpRequest.getHeader("x-forwarded-for");