【问题标题】:HAProxy1.7 forward client IP to backendHAProxy1.7 转发客户端IP到后端
【发布时间】:2020-11-25 12:59:51
【问题描述】:

我正在尝试使用 X_FORWARDED_FOR 将到达 haproxy 的 source/client_ip(%ci) 转发到应用程序。

HA-Proxy 版本 1.7.9 2017/08/18

global
    log 127.0.0.1 local1 info
    daemon
    user vcap
    group vcap
    maxconn 64000
    spread-checks 4

defaults
    maxconn 64000
    option http-server-close
    option httplog
    option forwardfor

frontend http-in
    mode http
    bind :80
    log global
    option forwardfor except 127.0.0.1

frontend https-in
    mode http
    log global
    bind :443 ssl crt /data/haproxy/ssl/server.pem
    option forwardfor except 127.0.0.1
    use_backend https-backend
    
    http-request set-header SSL_CLIENT_CERT_USER         %{+Q}[ssl_c_s_dn(cn)]
    http-request set-header SSL_CLIENT_CERT         %{+Q}[ssl_c_der,base64]
    http-request set-header SSL_CLIENT_CERT_USED    %[ssl_c_used] if no_user_cert

backend https-backend
    mode http
    log global
    balance roundrobin
    option forwardfor header X-Client

Java 应用程序执行以下操作:

String ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
 
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}

完成上述配置后,我在应用程序日志中获得了 haproxy 的 IP,而不是实际的源 IP。

在前端或后端或两侧添加 set-header 没有帮助: http-request set-header X-CLIENT-IP %[src].

我做错了吗?

【问题讨论】:

    标签: java reverse-proxy haproxy


    【解决方案1】:

    你应该更新到最新的 1.7 版本https://www.haproxy.org/bugs/bugs-1.7.9.html

    您定义了option forwardfor header X-Client,因此您的应用应该获得X-Client 标头。

    String ipAddress = request.getHeader("HTTP_X_CLIENT");
    

    文档:option forwardfor

    【讨论】:

    • 感谢 Aleksandar,我没有更改代码,而是尝试了 option forwardfor 而不是 option forwardfor header X-Client,默认情况下它具有“X-Forwarded-For”,并且提供了后端服务器的 IP。但是当我用header X-Client 替换标题时,它会提供haproxy IP。那么,您认为这是 1.7 中的错误吗?
    • 不知道,因为我没用过header ...选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2017-07-04
    • 2017-03-11
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多