How to rewrite domain.com to www.domain.com with HAProxy?

断了今生、忘了曾经 提交于 2019-12-06 22:39:49

问题


We have 1 loadbalancer with 3 members behind it:

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

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

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;
}

How can I manage this with 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!


回答1:


Revise the frontend block:

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 }

Source:

  1. Haproxy redirect www to non-www
  2. HAProxy 1.4 Manual
  3. Personal experience



回答2:


The HAProxy Configuration Manual answers this directly:

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 }

It's under the redirect entry:

  • HAProxy 1.7
  • HAProxy 1.6
  • HAProxy 1.5


来源:https://stackoverflow.com/questions/19156675/how-to-rewrite-domain-com-to-www-domain-com-with-haproxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!