2017-01-04 7 views
0

모든 트래픽을 루트 도메인에 www로 리디렉션하고 싶습니다. 여전히 경로/쿼리 문자열을 그대로 유지하면서 어떻게 할 수 있습니까? 내가 무엇을 놓치고 - example.com/abc?page=1 =>www.example.com/abc?page=1 : 나는 실제로 할 wan't example.com/abc?page=1 =>www.example.com :모든 루트 도메인 요청을 www로 리디렉션합니다. URL을 그대로 유지하는 하위 도메인

frontend http 
     bind *:80 
     bind *:443 

     acl has_www hdr_beg(host) -i www 
     http-request redirect code 301 location http://www.%[hdr(host)]%[req.uri] unless has_www 

그러나이 사람은 다음을 수행합니다 나는 HAproxy 설정 등은 다음과 설정을 가지고?

+0

어떤 버전의 HAProxy를 사용하고 있습니까? –

답변

1

당신이 HAProxy 1.5을 사용하고 최대이 작업을해야하는 경우에 당신

단일 도메인 : (더러운하지만 작동)

# match non www requests: 
acl has_www  hdr_beg(host) -i www. 

# add a header that says we need to redirect these type of requests (will explain later) 
http-request add-header X-Host-Redirect yes unless has_www 

# rule to identify newly added headers 
acl www_redirect hdr_cnt(X-Host-Redirect) eq 1 

# where the magic happens (insert www. in front of all marked requests) 
reqirep ^Host:\ (.*)$ Host:\ www.\1 if www_redirect 

# now hostname contains 'www.' so we can redirect to the same url 
    redirect scheme http if www_redirect 

:

acl has_www hdr_beg(host) -i www 
redirect prefix http://www.example.com code 301 unless has_www 

여러 도메인 새 헤더를 추가하는 이유는 HAProxy 1.5 이상에서 ACL이 bei 각 참조에 대해 평가되었습니다. 그래서 새로운 헤더없이 이것을하려고 할 때 :

#catch all domains that begin with 'www.' 
acl has_www  hdr_beg(host) -i www. 
#insert www. in front of all non www requests 
reqirep ^Host:\ (.*)$ Host:\ www.\1 unless has_www 
#now hostname contains 'www.' so when the next rule gets interpreted it does not match then fails 
#redirect to new url 
redirect code 301 prefix/if has_www 

희망이 도움이 될 것입니다. 나는 HAProxy 1.5에 5 개 개의 다른 도메인으로이 테스트를했는데 그것은 잘 작동하고 경로 및 쿼리 매개 변수를 보존 리디렉션하는 HAProxy 1.6 리디렉션

+0

하지만 example.com뿐만 아니라 모든 도메인 이름에서 작동해야합니다. a_domain.se/abc?page=1 => www.a_domain.se/abc?page=1 –

+0

여러 도메인을 포함하도록 답변을 업데이트했습니다. –

+0

@LouisKriek 어떻게하면 리디렉션 만할까요? 루트 도메인, 지. 'example.com -> www.example.com'이 아닌'sub1.example.com'입니까? – TylerDurden

0

멀티 도메인 솔루션에 그대로 쿼리 문자열을 유지 :

frontend 443 
    http-request redirect prefix https://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }