Haproxy将www重定向到非www

编程入门 行业动态 更新时间:2024-10-23 13:31:59
本文介绍了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到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

本文发布于:2023-07-16 03:49:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Haproxy   www

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!