我已经配置好了haproxy
,当我输入时test.example.com
它会重定向到example.com/test
这就是我想要的,但问题是,我希望这在不改变浏览器上的 url 的情况下发生,我希望它保留,test.example.com
但将请求发送到example.com/test
这是我的haproxy.cfg
frontend tt
bind *:80
bind *:443
mode http
default_backend ss
bind :443 ssl crt /etc/ssl/certs/ssl.pem
http-request redirect scheme https unless { ssl_fc }
backend ss
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
http-request set-var(req.rewrite_example) req.hdr(host),lower,regsub(\.example\.com$,) if { hdr_end(host) -i .example.com }
http-request set-path /%[var(req.rewrite_example)] if { var(req.rewrite_example) -m found }
http-request set-header Host example.com if { var(req.rewrite_example) -m found }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web1 example.com:80
答案1
您的配置几乎可以正常工作,请检查以下几项更改:
# ## example_fe ##
frontend example_fe
bind *:80
bind *:443 ssl crt /etc/ssl/certs/ssl.pem
http-request redirect scheme https unless { ssl_fc }
mode http
default_backend example_be
# ## example_be ##
backend example_be
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
http-request set-var(req.rewrite_example) req.hdr(host),lower,regsub(\.example\.com$,) if { hdr_end(host) -i .example.com }
http-request set-path /%[var(req.rewrite_example)]%[path] if { var(req.rewrite_example) -m found }
http-request set-header Host example.com if { var(req.rewrite_example) -m found }
server web1 172.17.1.1:80
我已经在 HAProxy 2.4 版上测试过,运行良好。以上配置由 Michael 提供:https://serverfault.com/a/815668/372988