0

I am trying to have HA-Proxy 301 redirect an https domain. I need:

https://example.com to redirect to https://www.example.com

This is what I was trying to get it to work:

# corp - example.com
    redirect prefix https://www.example.com code 301 if { hdr(host) -i example.com }
    redirect prefix https://www.example.com code 301 if { hdr(host) -i www.example.com }
    acl server01 hdr_dom(host) -i www.example.com

I would greatly appreciate any help!

EDITED EXAMPLE OF cfg FILE (Code at bottom is part of cfg):

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon

defaults log global maxconn 100000 mode http option httplog option dontlognull option forwardfor timeout connect 600000ms timeout server 600000ms timeout client 600000ms errorfile 400 /etc/haproxy/errors/400.http errorfile 403 /etc/haproxy/errors/403.http errorfile 408 /etc/haproxy/errors/408.http errorfile 500 /etc/haproxy/errors/500.http errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http errorfile 504 /etc/haproxy/errors/504.http

frontend main_http_proxy bind *:80 mode http http-request add-header X-Forwarded-Proto https if { ssl_fc } stats enable stats uri /haproxystatus?stats stats realm Strickly\ Private stats auth ec2ops:7is92kc81k92kds8

# example.com
#redirect scheme https code 301 if { hdr(host) -i www.example.com } !{ ssl_fc }
#redirect prefix  https://www.example.com code 301 if { hdr(host) -i example.com } !{ ssl_fc }
#redirect prefix https://www.example.com code 301 if { hdr(host) -i example.com }
#redirect prefix https://www.example.com code 301 if { hdr(host) -i www.example.com }
acl no_WWW hdr(host) -i example.com
redirect prefix www.example.com code 301 if no_WWW


# Backend Rules
use_backend SERVER01 if jcma01
use_backend SERVER01 if no_WWW

frontend main_https_proxy bind :443 ssl crt /etc/haproxy/pem/ mode http http-request add-header X-Forwarded-Proto https if { ssl_fc } acl secure dst_port eq 443 rsprep ^Set-Cookie:\ (.) Set-Cookie:\ \1;\ Secure if secure rspadd Strict-Transport-Security:\ max-age=31536000 if secure

use_backend SERVER01 if { ssl_fc_sni www.example.com } #use_backend SERVER01 if { ssl_fc_sni example.com }

backend server01 balance source cookie SERVER01 insert server SERVER01 10.10.10.10:80 cookie SERVER01 check inter 5s

1
  • With the current config, does the 'www' URL rewrite work (without any HTTPS redirects)? Does HTTPS redirection work (without the 'www' URL rewrite)?
    – Jim G.
    Jul 21, 2015 at 16:24

2 Answers 2

1

Not sure where you got those ACLs, wouldn't

redirect scheme https code 301 if !{ ssl_fc }

or

redirect scheme https code 301 if {hdr(host) -i yourdomain.example.com } !{ ssl_fc }

be a cleaner solution?

6
  • That would work with non-https redirects, but when you explicitly go to 'https ://example.com', it does not redirect to 'https ://www.example.com'.
    – Brandman
    Jul 20, 2015 at 19:56
  • Ah, I reread the question, and I understand now. Why not separate the redirect for www and the HTTPS redirect. Add ' acl no_WWW hdr(host) -i example.com' and 'redirect prefix example.com code 301 if no_WWW'
    – Jim G.
    Jul 20, 2015 at 20:04
  • Hey Jim, I really appreciate the help, would you mind elaborating? Im still confused. Where is that code you suggested supposed to be placed exactly?
    – Brandman
    Jul 20, 2015 at 20:58
  • Can you post a full redacted config? That will make things easier.
    – Jim G.
    Jul 20, 2015 at 21:23
  • Sure, Ill edit my original question with it.
    – Brandman
    Jul 21, 2015 at 14:47
0

I know it's old, but now the answer can be found in the Haproxy official documentation:

frontend www
   bind :443
   acl has_www hdr_beg(host) -i www
   http-request redirect  code 301  location https://www.%[hdr(host)]%[capture.req.uri]  unless has_www
   use_backend webservers

https://www.haproxy.com/documentation/hapee/latest/traffic-routing/redirects/#redirect-traffic-to-a-location

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .