ubuntu – How to Redirect Rewritten URL Using HAProxy

haproxyUbuntu

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

Best Answer

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/