HAProxy – 如何在X-Client-IP和X-Forwarded-For头添加客户端IP?

我有一个HAProxy服务器的问题。 我想在头部转发一个客户端IP。 我几乎做到了,但是有一个有趣的案例,我无法弄清楚。 我需要在X-CLIENT-IP和X-FORWARDED-FOR标签中的头2个地方编写客户端IP。

问题是:当我使用

option http-server-close option forwardfor 

在目标服务器上,我看到头X-FORWARDED-FOR = xxx.xxx.xxx.xxx(客户端IP),但没有x-client-ip头。

当我使用:

 option forwardfor header X-Client-IP option http-server-close 

在目标服务器上,我看到头X-CLIENT-IP = xxx.xxx.xxx(客户端IP),但是X-FORWARDED-FOR = xxx.xxx.xxx.xxx(HAProxy ip)

我需要在目标标题上看到X-CLIENT-IP和X-FORWARDED-FOR具有客户端IP的值。

我尝试混合configuration

  option forwardfor option forwardfor header X-Client-IP option http-server-close 

没有效果。 我也不能安装任何模块。 目标是IIS。

有任何想法吗? 🙁

您可以尝试设置自定义标题,如下所示:

 http-request set-header X-Client-IP %[src] 

或者,你甚至可以从X-Forwarded-For头文件复制它,我认为语法会像这样:

 http-request set-header X-Client-IP req.hdr_ip([X-Forwarded-For]) 

如果你想同时使用,你需要添加一个http-request关键字。

 # add X-FORWARDED-FOR option forwardfor # add X-CLIENT-IP http-request add-header X-CLIENT-IP %[src] 

您可以在后端服务器上安装一个名为mod_rpaf的模块。 这将X-FORWARDED-FOR IP复制到X-CLIENT-IP。 欲了解更多信息,请参阅。 在Windows上,你应该有类似的模块,像X-Forwarded-For ASAPIfilter。