HAProxy, as is name suggest, is a proxy server and a load balancer for provide high availability. In this article first I will show how to install HAProxy in FreeBSD. Next we will set up to act as a reverse proxy.
The install
In FreeBSD I like to build and install my software from the ports tree, so run as root:
# cd /usr/ports/net/haproxy
# make install clean
Pay attention to the screen for if some options dialog popup (mostly from dependencies). Enable HAProxy in /etc/rc.conf when installed and continue with the next section :
# /usr/local/etc/rc.d/haproxy enable
haproxy enabled in /etc/rc.conf
The configuration
Before we proceed editing the configuration file, let me explain an scenario:
First we have only one available public IP. Secondly, we have a couple of services we want to expose on internet, say an app (webapp) and our website. Both are running on their own servers. Finally we have a third spare server where we already installed HAProxy in the previous section and this will be the one exposed on the internet.
An user will point its browser to http://haproxy/website or http://haproxy/app and haproxy will sent the query to the correct server and return the results to the browser.
________ /------- [server A] [ client browser] -----> | haproxy |-----+ +----------+ \--------[server B]
Anyway, wikipedia tell its better how a reverse proxy works.
Add the following sections to /usr/local/etc/haproxy.conf. To start the global default values are fine:
frontend http-in bind *:80 option forwardfor use_backend uno if { path_beg /app} use_backend dos if { path_beg /www } backend uno mode http server nodea ipAp.or.fqdn:port backend dos mode http server nodeb ipB.or.fqdn:port
If you, as me, don’t have an example haproxy.conf this add this section to the top of the file:
global daemon maxconn 4096 defaults log global mode http timeout connect 5s timeout client 10s timeout server 10s
With this minimal configuration you can check if evertyhing is ok and start the service:
For testing purposes I’ve made haproxy to get things from my own pc:
I have an Apache where I store some programs that I could need when I’m on someone else’s PC. The magic word is the full url for some of those programs.
What about some real use of HAProxy?
This is a minimal example, not very useful by the way. I strongly recommend to read at least two documents of HAProxy:
- Starter guide, and the
- Configuration manual
And your haproxy.conf file would be as complicated or simple as needed.