Skip to content

Instantly share code, notes, and snippets.

@krast
Forked from meineerde/haproxy.cfg
Created November 29, 2016 16:39
Show Gist options
  • Save krast/14e3ea8060d212237fb3ac373c91f3f4 to your computer and use it in GitHub Desktop.
Save krast/14e3ea8060d212237fb3ac373c91f3f4 to your computer and use it in GitHub Desktop.
HAProxy: Route requests based on an environment variable
frontend http
bind 10.0.0.1:80
mode http
# Add a new header with the environment variable and the path concatenated
http-request set-header X-ROUTING %[env(SERVER_AUTH)]::%[path]
# Then compare the value using a regular expression with back-references
# We need to use a named capture because of "reasons". Backreferences to normal captures seem to be rejected by HAProxy
use_backend app if { req.hdr(X-ROUTING) -m reg ^(?<auth>.+)==\1 }
# Cleanup in case we hadn't matched yet
http-request del-header X-ROUTING
default_backend default
backend app
# ...
backend default
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment