如何在响应中使用其他标题重定向?

时间:2019-06-12 08:46:26

标签: redirect haproxy cache-control

当前,我使用haproxy将某些域从https自动升级到https。我使用代码308,因为我找不到添加Expires: Thu, 01 Dec 2014 16:00:00Cache-Control: max-age=3600标头的可能性。

frontend http-frontend
    bind *:80
    mode http
    redirect scheme https code 307 if { hdr(Host) -i foo.example.com } !{ ssl_fc }
    redirect scheme https code 307 if { hdr(Host) -i bar.example.com } !{ ssl_fc }

如何修改上述重定向规则,以在响应中包含Cache-Control标头?

1 个答案:

答案 0 :(得分:2)

HAProxy通过“ http-request set-header”指令插入标头。 不幸的是,在撰写本文时,仅当HAProxy从后端获取流量时才可用。
“ http-request redirect ..”(或“ redirect”)是前端上的最终指令,在这种情况下,没有后端响应。 解决方法是创建一个虚拟后端,如下所示:

frontend http-frontend
    bind *:80
    mode http
    use_backend be_dummy_redirect if { hdr(Host) -i foo.example.com } !{ ssl_fc }
    use_backend be_dummy redirect if { hdr(Host) -i bar.example.com } !{ ssl_fc }
    default_backend be_app

 backend be_dummy_redirect
    http-response set-header Expires "Thu, 01 Dec 2014 16:00:00"
    http-response set-header Cache-Control max-age=3600
    server redirect-server 127.0.0.1:9000

 listen redirect
    bind 127.0.0.1:9000
    http-request redirect scheme https

这是discussed之前的Devs,也许将来会实现