Skip to content

Instantly share code, notes, and snippets.

@meineerde
Created June 23, 2015 09:44
Show Gist options
  • Save meineerde/8bea63e64fc47f9a67c0 to your computer and use it in GitHub Desktop.
Save meineerde/8bea63e64fc47f9a67c0 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