如何使用HAProxy将domain.com重写到www.domain.com?

时间:2013-10-03 10:21:55

标签: rewrite haproxy

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

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

目前我们在loadbalancer上使用nginx,但我们想用HAProxy替换它。

Nginx使用以下行将没有www(domain.com)的域名重写为www.domain.com:

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

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

如何使用HAproxy进行管理?

我的haproxy配置:

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!

2 个答案:

答案 0 :(得分:19)

修改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 redirect www to non-www
  2. HAProxy 1.4 Manual
  3. 个人经历

答案 1 :(得分:3)

HAProxy配置手册直接回答:

Example:

Append 'www.' prefix in front of all hosts not having it

http-request redirect code 301 location      \
  http://www.%[hdr(host)]%[capture.req.uri]  \
  unless { hdr_beg(host) -i www }

它位于redirect条目下:

相关问题