我目前正在使用Haproxy来平衡多个express.js节点.我知道可以使用express.js进行重定向,但是我希望使用Haproxy进行重定向.
I'm currently using Haproxy to balance several express.js nodes. I know that it's possible to redirect using express.js, but I was hoping to do so with Haproxy.
我想知道如何进行从www.mysite到mysite的永久重定向?
I was wondering how I can do a permanent redirect from www.mysite to mysite?
推荐答案redirect prefix example code 301 if { hdr(host) -i www.example }
请参阅重定向前缀的文档a>有关更多信息的规则.
Please see the documentation of the redirect prefix rule for more information.
如果您使用的是更高版本的HAProxy,即至少为1.6,则可以使用更通用的语法,该语法允许重定向任何主机,而不仅仅是显式命名
If you are using a newer version of HAProxy, i.e. at least 1.6, you can use a more generic syntax which allows to redirect any host, not just explicitly named
http-request redirect prefix %[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }在这里,我们使用的是 regsub 过滤器以动态生成不带www.前缀的正确主机名.
Here, we are using the regsub filter to dynamically generate the correct hostname without the www. prefix.
如果您想以其他方式执行重定向,即添加www(如果还没有),则规则变得更简单:
In case you want to perform a redirect the other way around, i.e. to add a www if there is none already, the rule becomes simpler:
http-request redirect prefix www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }更多推荐
Haproxy将www重定向到非www
发布评论