无法在HAProxy中设置标题

时间:2019-04-09 22:56:17

标签: cors http-headers haproxy

我正在尝试以此覆盖响应头中的 Access-Control-Allow-Origin (如在浏览器的网络监视器中)

  

访问控制允许来源:*

对此

  

访问控制允许来源:https://my-domain.com

当我每次访问https://my-domain.com:8081的网站时,当请求的资源是图像( .png)或字体( .woff / 2)时。图像的请求的“主机:”标头是 my-domain.com ,而字体的是 fonts.gstatic.com

由于某种原因,当请求的资源是woff字体时,标头不会被覆盖-尝试使用“#NOK”下的所有组合,如下所述。

我可能会丢失什么?

frontend HEADERS
    bind *:8081 ssl crt my_domain.pem
    capture request header origin len 128

    acl hostname hdr(host) -i my-domain.com:8081
    acl hostname_font hdr(host) -i fonts.gstatic.com
    acl images path_end .png
    acl fonts path_end .woff .woff2

    # OK
    use_backend SET_HEADER if hostname images

    # NOK
    # use_backend SET_HEADER if hostname_font fonts
    # use_backend SET_HEADER if fonts
    # use_backend SET_HEADER if hostname_font

    # Default
    use_backend NO_SET_HEADER

backend NO_SET_HEADER
    mode http
    balance leastconn

    http-request set-header X-Forwarded-Proto https if { ssl_fc }
    http-request set-header X-Forwarded-Host %[req.hdr(Host)]
    http-request set-header Host some-other-domain.com

    server SOME-OTHER-DOMAIN-BE-01 10.10.0.5:443 ssl verify none check weight 1
    server SOME-OTHER-DOMAIN-BE-02 10.10.0.6:443 ssl verify none check weight 1

backend SET_HEADER
    mode http
    balance leastconn

    http-response set-header Access-Control-Allow-Origin https://my-domain.com

    http-request set-header X-Forwarded-Proto https if { ssl_fc }
    http-request set-header X-Forwarded-Host %[req.hdr(Host)]
    http-request set-header Host some-other-domain.com

    server SOME-OTHER-DOMAIN-BE-01 10.10.0.5:443 ssl verify none check weight 1
    server SOME-OTHER-DOMAIN-BE-02 10.10.0.6:443 ssl verify none check weight 1

1 个答案:

答案 0 :(得分:0)

正如@mweiss所提到的,这实际上是预期的行为-对fonts.gstatic.com的请求永远不会“降落”在HAProxy主机上。因此,无法编辑标题。