Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active February 7, 2023 14:55
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save drmalex07/10d09c299245e3ab333c to your computer and use it in GitHub Desktop.
Save drmalex07/10d09c299245e3ab333c 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
@mcmacerson
Copy link

This works perfectly! Full champion. For help with HTTPS go here:
https://serverfault.com/questions/738045/haproxy-to-terminate-ssl-also-send-ssl-to-backend-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment