Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chrishuan9/07dd9aa4d227d595c91f829ba0e19c9a to your computer and use it in GitHub Desktop.
Save chrishuan9/07dd9aa4d227d595c91f829ba0e19c9a to your computer and use it in GitHub Desktop.
Perform simple reverse-proxying in HAProxy. #reverse-proxy #proxy #haproxy #proxypass

A more complete example (with rewriting cookie domains/paths) can be found at http://blog.haproxy.com/2014/04/28/howto-write-apache-proxypass-rules-in-haproxy/

We will try something roughly equivalent to the following ProxyPass directives in Apache2:

ServerName www.example.com
...
ProxyPass        /foo/  http://foo.local
ProxyPassReverse /foo/  http://foo.local

In haproxy.cfg we define a backend, say foo, to reverse-proxy to foo.local backend server.

frontend http-in
    bind 0.0.0.0:80
    acl prefixed-with-foo  path_beg /foo/
    acl host-is-www-example    hdr(host) eq www.example.com
    use_backend foo-backend if  host-is-www-example prefixed-with-foo
    # other rules ...
    default_backend default-backend

backend foo-backend
    http-request set-header Host foo.local
    server node1 foo.local:80
    # Map url path as ProxyPass does
    reqirep  ^(GET|POST|HEAD)\ /foo/(.*)     \1\ /\2
    # Rewrite redirects as ProxyPassReverse does
    acl response-is-redirect res.hdr(Location) -m found
    rspirep ^Location:\ (http|https)://foo.local\/(.*)   Location:\ \1://www.example.com/foo/\2  if response-is-redirect
    #option forwardfor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment