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

13

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

主负载均衡器:www.website.com 成员:web1.website.com、web2.website.com和web3.website.com

目前我们在负载均衡器上使用的是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

谢谢!

2个回答

26

如何在haproxy中隐藏index.php的URL? - Nullpointer

6
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”条目下:

这将会把 blog.example.com 重定向到 www.blog.example.com。有人知道如何避免这种情况吗? - srigi
我猜你可以使用 unless { hdr_beg(host) -i www } OR unless { hdr_beg(host) -i blog} - Allen King

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接