为什么 HAProxy 不添加“X-Forwarded-For”?

pio*_*oto 5 haproxy

我在网上查阅了大量参考资料,这些参考资料告诉我,我应该能够设置一个前端,例如:

frontend http_https
  bind 1.2.3.4:443 ssl crt /etc/haproxy/tls/mycert.pem
  bind 1.2.3.4:80
  mode http
  option httplog
  option forwardfor
  option http-server-close
  redirect scheme https code 301 if !{ ssl_fc }
  http-request set-header X-Forwarded-Port %[dst_port]
  http-request add-header X-Forwarded-Proto https if { ssl_fc }
  http-request set-header X-Request-Start t=%Ts%ms
  http-response set-header Strict-Transport-Security max-age=15768000
  use_backend %[req.hdr(host),lower,map(/etc/haproxy/domains-to-backends.map)]
Run Code Online (Sandbox Code Playgroud)

当我在后端服务器上获取 a 时tcpdump,我看到所有add-headerset-header标头,但没有看到任何X-Forwarded-For标头。

如果我添加类似的内容:

  http-request add-header X-Client-IP %[src]
Run Code Online (Sandbox Code Playgroud)

我确实看到该标头连同正确的客户端 IP 一起传递。

任何人都可以深入了解为什么这可能行不通吗?或者有什么理由我不应该只使用http-request set-header X-Forwarded-For %[src]而不是option forwardfor


后端部分示例:

backend bk_foo
  balance roundrobin
  errorfile 502 /etc/haproxy/errorfiles/502.http
  errorfile 503 /etc/haproxy/errorfiles/503.http
  errorfile 504 /etc/haproxy/errorfiles/504.http
  option httpchk
  server foo.example.com 10.1.2.3:8080 check
Run Code Online (Sandbox Code Playgroud)

ant*_*ter 5

编辑:您应该放入option forwardfor后端部分。不在前端部分下。

我不认为 HAProxy 配置有任何问题。我已经使用forwardfor选项完成了许多配置,没有任何问题。HAProxy 和后端服务器之间是否存在可能删除某些标头的设备?另外,我建议使用一个简单的设置来测试这个标头,以确保其他配置不会导致问题。