Using haproxy version 1.4.24 on Ubuntu 14.04

I would like to use haproxy to rewrite and redirect urls which are no longer valid to their new equivalent. I am already using haproxy as the reverse proxy and load balancer. I have the rewrite working correctly but when I try to redirect I either end up with no redirect or in a redirect loop. I read on a few places that a technique for this is to rewrite and redirect the url using prefix in the frontend but I'm not having any luck. Any help would be appreciated. Thanks.

global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    maxconn 4096
    user haproxy
    group haproxy
    spread-checks 0

defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    timeout queue 20000
    timeout client 50000
    timeout connect 5000
    timeout server 50000

frontend haproxy-0-80
    bind 0.0.0.0:80
    default_backend haproxy_service
    acl old_url path_beg -i /post
    # The below doesn't appear to have any effect
    reqrep ^([^\ :]*)\ /post/\d+/(.+)/?     \1\ /\2
    redirect prefix http://10.0.3.10 code 301 if old_url

backend haproxy_service
    balance leastconn
    cookie SRVNAME insert
    # The below properly handles the rewrite
    reqrep ^([^\ :]*)\ /post/\d+/(.+)/?     \1\ /\2
    server ghost-0-2368 10.0.3.220:2368 maxconn 100 cookie S0 check
  • I could upgrade to the 1.15.x version if this isn't possible in 1.14.x – hatch Dec 13 '14 at 23:39
up vote 8 down vote accepted

As it turns out I was very close to a working config.

Changing the redirect line to read redirect prefix / code 301 if old_url made it work as expected.

I wrote a blog post which outlines the problem in a little more detail: https://fromanegg.com/post/2014/12/05/how-to-rewrite-and-redirect-with-haproxy/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.