大网数据广告活动
服务器运维问答

HAProxy add some headers before 302 redirect

· 52CE 我爱测

I'm trying to add some security headers to the responses that are directed to a specific port. I have the following configured frontend: frontend desenv_ext_1 bind *:80 bind *:443 ssl crt /etc/ssl/certs/cert.pem mode http option …

问题原文

原帖链接 · R Wagner · CC BY-SA 4.0

I'm trying to add some security headers to the responses that are directed to a specific port. I have the following configured frontend:

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 }

You see, in the configuration you have some tests in different ways, and neither of these ways works.

The redirect is working correctly, but the headers are not added in the port 80 response:

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

I would like requests arriving on port 80 to have the following headers added, even if they have a redirect to port 443:

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

The output that I need is this:

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:

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

My HA-Proxy version 1.5.18

已采纳解答

原帖链接 · nuster cache server · CC BY-SA 4.0

redirect gets executed before http-response, so these http-response s never get executed.

use this:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。