【问题标题】:HAProxy add some headers before 302 redirectHAProxy 在 302 重定向之前添加一些标头
【发布时间】:2018-11-24 14:41:47
【问题描述】:

我正在尝试将一些安全标头添加到定向到特定端口的响应中。 我有以下配置的前端:

frontend desenv_ext_1 
bind *:80 
bind *:443  ssl crt /etc/ssl/certs/cert.pem 
mode http 
option tcplog 
default_backend desenv_1 
timeout client          5m

#ACL to new attempt 
acl header_c dst_port 80

#Attempt with no ACL 
http-response set-header X-Frame-Options SAMEORIGIN 

#Attempt with ssl ACL 
http-response set-header Strict-Transport-Security max-age=31535400;\ includeSubDomains;\ preload; if {ssl_fc} 
http-response add-header Referrer-Policy no-referrer if !{ ssl_fc }

#Attempt with header_c ACL 
http-response set-header X-Content-Type-Options nosniff if header_c 
http-response add-header X-XSS-Protection 1;\ mode=block if header_c

#Attempt with rspadd 
rspadd X-Backen-Serve\ laranja if header_c 
rspadd X-Backend-Serve\ caju if HTTP

redirect scheme https if !{ ssl_fc }

你看,在配置中你有一些不同方式的测试,但这些方式都不起作用。

重定向工作正常,但未在端口 80 响应中添加标头:

[root@managerr temp]# curl -I http://localhost
HTTP/1.1 302 Found
Cache-Control: no-cache
Content-length: 0
Location: https://localhost/
Connection: close

我希望到达端口 80 的请求添加以下标头,即使它们重定向到端口 443:

Strict-Transport-Security max-age=31535400;\ includeSubDomains;\ preload;
X-Frame-Options SAMEORIGIN
X-Content-Type-Options nosniff
X-XSS-Protection 1;\ mode=block

我需要的输出是这样的:

HTTP/1.1 302 Found
Strict-Transport-Security max-age=31535400;\ includeSubDomains;\ preload;
X-Frame-Options SAMEORIGIN
X-Content-Type-Options nosniff
X-XSS-Protection 1;\ mode=block
Cache-Control: no-cache
Content-length: 0
Location: https://localhost/
Connection: close

后端:

backend desenv_1
mode http
option tcplog
server manga x.x.x.x:80 check cookie manga
timeout connect        10s
timeout server          5m

我的 HA-Proxy 版本 1.5.18

【问题讨论】:

    标签: redirect header add haproxy


    【解决方案1】:

    redirecthttp-response 之前被执行,所以这些http-response 永远不会被执行。

    使用这个:

    http-request redirect location "https://%[hdr(host)]%[url]\r\nX-Frame-Options: SAMEORIGIN\r\nReferrer-Policy: no-referrer"
    

    【讨论】:

    • 这是一个聪明的技巧,但会破坏通过 HTTP/2 连接的客户端,例如在卷曲http2 error: Invalid HTTP header field was received: frame type: 1, stream: 1, name: [location], value: [...] PROTOCOL_ERROR。此处提供了更可靠的解决方法:How to redirect with additional headers in the response?
    猜你喜欢
    • 2016-04-12
    • 2010-10-15
    • 2013-04-06
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多