如何使用HAProxy将domain.com重写为www.domain.com? [英] How to rewrite domain.com to www.domain.com with HAProxy?

查看:79
本文介绍了如何使用HAProxy将domain.com重写为www.domain.com?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有1个负载均衡器,后面有3个成员:

We have 1 loadbalancer with 3 members behind it:

主平衡器:www.website.com 成员:web1.website.com,web2.website.com和web3.website.com

main balancer: www.website.com members: web1.website.com, web2.website.com and web3.website.com

当前,我们在负载均衡器上使用nginx,但我们希望将其替换为HAProxy.

Currently we are using nginx on the loadbalancer, but we want to replace it with HAProxy.

Nginx使用以下行将不带www(domain.com)的域重写为www.domain.com:

Nginx rewrites the domain without www (domain.com) to www.domain.com with the following line:

server {
    server_name domain.com;
    listen 1.2.3.4:80;

    rewrite ^(.*) http://www.domain.com$1 permanent;
}

我该如何使用HAproxy进行管理?

How can I manage this with HAproxy?

我的haproxy配置:

My haproxy config:

frontend http 1.2.3.4:80

    default_backend www_cluster
    acl is_www hdr_end(host) -i www.domain.com
    use_backend www_cluster if is_www


backend www_cluster

    balance roundrobin
    cookie SERVERID insert nocache indirect

    option httpchk HEAD / HTTP/1.0
    option httpclose
    option forwardfor

    server web1 1.2.3.5:82 cookie WEB1 check
    server web2 1.2.3.6:82 cookie WEB2 check
    server web3 1.2.3.7:82 cookie WEB3 check

TIA!

推荐答案

修改frontend块:

frontend http 1.2.3.4:80
    default_backend www_cluster
    redirect prefix http://www.mydomain.com code 301 if { hdr(host) -i domain.com }

来源:

  1. Haproxy将www重定向到非www
  2. HAProxy 1.4手册
  3. 个人经历
  1. Haproxy redirect www to non-www
  2. HAProxy 1.4 Manual
  3. Personal experience

这篇关于如何使用HAProxy将domain.com重写为www.domain.com?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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