【问题标题】:How to rewrite domain.com to www.domain.com with HAProxy?如何使用 HAProxy 将 domain.com 重写为 www.domain.com?
【发布时间】:2013-10-10 00:04:10
【问题描述】:

我们有 1 个负载均衡器,后面有 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

TIA!

【问题讨论】:

    标签: rewrite haproxy


    【解决方案1】:

    修改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. 个人经历

    【讨论】:

    • 如何在 haproxy 的 url 中隐藏 index.php
    【解决方案2】:

    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。有谁知道如何避免这种情况?
    • 我猜你可以使用unless { hdr_beg(host) -i www } OR unless { hdr_beg(host) -i blog}
    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多