How to install HAProxy on FreeBSD

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"

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
screenshot of the end of haproxy port compilation and install
The compile and install should end quickly

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:

    maxconn 4096

   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:

now open a browser and test

For testing purposes I’ve made haproxy to get things from my own pc:

screenshot of firefox trying to browse via haproxy
I didn’t said the magic word

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:

And your haproxy.conf file would be as complicated or simple as needed.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"
Gonzalo Rivero
Gonzalo Rivero
I am Gonzalo, I live in Salta, a city located in the NW of Argentina. I play the guitar and a little harmonica. I also like to bike.


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook