Haproxy将www重定向到非www [英] Haproxy redirect www to non-www

查看:73
本文介绍了Haproxy将www重定向到非www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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.com到mysite.com的永久重定向?

I was wondering how I can do a permanent redirect from www.mysite.com to mysite.com?

推荐答案

redirect prefix http://example.com code 301 if { hdr(host) -i www.example.com }

请参阅重定向前缀的文档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 http://%[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 http://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }

这篇关于Haproxy将www重定向到非www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆