【发布时间】:2023-07-06 23:32:01
【问题描述】:
我正在寻找与下面这个后端代码块等效的对 www.example.com 和 example.com 的请求。
http-response set-header X-Target example.com
server web-servers site.example.com:80 check
我将所有请求发送到 www.example.com,但我想使用 haproxy 将它们发送到 site.example.com。 example.com 有几种变体,所以我想要一个允许的域列表,然后如果它们被允许,我想要一个如下所示的后端代码块,我可以在其中使用 %[req.hdr(Host)]作为 http-response X-Target 语句中的值。
http-response set-header X-Target %[req.hdr(Host)]
server web-servers site.%[req.hdr(Host),regsub(^www.,,)]:80 check
HA-Proxy 版本 2.1.4-273103-54 2020/05/07 - https://haproxy.org/
我在尝试 haproxy -c -f haproxy.test 时遇到此错误
[root@pm-prod-haproxy05 haproxy]# haproxy -c -f haproxy.test [ALERT] 259/180932 (16116) : parsing [haproxy.test:40]: ‘http-response set-header’: 此处可能无法可靠地使用示例获取,因为它需要此处不可用的 ‘HTTP request headers’。 [警报] 259/180932 (16116):在配置文件中发现错误:haproxy.test [root@pm-prod-haproxy05 haproxy]#
我也试过这个:
http-request set-header X-Target %[req.hdr(Host)]
http-request set-header X-Hostname %[req.hdr(Host),regsub(^www.,site.,)]
http-request web-server do-lookup(hdr(X-Hostname))
server web-servers web-server:80 check
这是我的完整配置。
global
log 127.0.0.1 local2 debug
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
option httplog
log global
option dontlognull
option http-server-close
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend frontend-http
bind *:80
bind *:443
acl redirect path_beg -i /rd
use_backend backend-tracking if redirect
default_backend backend-default
backend backend-default
option forwardfor
http-response set-header X-Publishing-system website
http-response set-header X-Target %[req.hdr(Host)]
server web-servers site.%[req.hdr(Host),regsub(^www.,,)]:80 check
backend backend-tracking
option forwardfor
http-response set-header X-Publishing-system redirect
http-request set-uri %[url,regsub(^/rd,/,)]
server web-hp www.trackingplatform.com:80 check
【问题讨论】:
-
您在哪个部分添加了 http-response 行,因为它仅在 fe,be 和 listen 中可用。 cbonte.github.io/haproxy-dconv/1.8/… 。请分享您的 haproxy 配置的更多部分。
-
我增加了一些配置。我只需要在服务器指令中将 www 与站点切换。 site.%[req.hdr(Host),regsub(^www.,,)]
标签: haproxy