Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Created August 22, 2022 13:41
Show Gist options
  • Save haproxytechblog/9338713adb7340e2201256adbb4a2dc4 to your computer and use it in GitHub Desktop.
Save haproxytechblog/9338713adb7340e2201256adbb4a2dc4 to your computer and use it in GitHub Desktop.
Path-based Routing with HAProxy
frontend mysite
bind :80
# route to a backend based on path's prefix
use_backend app-a if { path /a } || { path_beg /a/ }
use_backend app-b if { path /b } || { path_beg /b/ }
backend app-a
# strip the prefix '/a' off of the path
http-request replace-path /a(/)?(.*) /\2
server server1 127.0.0.1:8080 check maxconn 30
backend app-b
# strip the prefix '/b' off of the path
http-request replace-path /b(/)?(.*) /\2
server server1 127.0.0.1:8081 check maxconn 30
use_backend app-a if { path /a } || { path_beg /a/ }
use_backend app-b if { path /b } || { path_beg /b/ }
use_backend app-a if { path /a /c /d } || { path_beg /a/ /c/ /d/ }
acl app-a-path path /a /c /d
acl app-a-path path_beg /a/ /c/ /d/
use_backend app-a if app-a-path
backend app-a
http-request replace-path /a(/)?(.*) /\2
server server1 127.0.0.1:8080 check maxconn 30
backend app-b
http-request replace-path /b(/)?(.*) /\2
server server1 127.0.0.1:8081 check maxconn 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment