Linux Commands and Scripts

Steps to Install HAProxy Logging with Rsyslog

In this article, we will see how to install HAProxy logging with Rsyslog on CentOS 8.

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is used to load balance applications by distributing requests between multiple servers, and to ensure that applications are highly available for users.

By default, HAProxy is not installed in CentOS 8 to write its log output to a file. This article will help you to install and configure HAProxy logging with Rsyslog on CentOS 8.

Step 1 – Keep the server up to date

# dnf update -y

Step 2 – Install HAProxy

The following command will install HAProxy using the dnf command and enable the service.

#  sudo dnf install haproxy -y

Once the installation gets complete, start and enable haproxy.service using systemctl command.

# systemctl start haproxy.service

# systemctl enable haproxy.service

Once you have confirmed that HAProxy is enabled and running, you can continue to the next step, which is configuring HAProxy’s logging directives.

Step 3 – Configure HAProxy Logging Directives

To configure HAProxy’s logging directives, open /etc/haproxy/haproxy.cfg in your preferred editor:

# sudo vi /etc/haproxy/haproxy.cfg

Find log local2 line and comment it with # and add new following line.

log         /dev/log local0

Save and exit.

The last step that you need to complete in this section is to create the /var/lib/haproxy/dev directory since it does not exist by default.

Create the directory using the mkdir command and then restart HAProxy:

# sudo mkdir /var/lib/haproxy/dev

# systemctl restart haproxy.service

We have successfully configured HAProxy to send its log to /var/lib/haproxy/dev/log.

Step 4 – Configure Rsyslog to Collect HAProxy Logs

By default, in CentOS 8 Rsyslog does not handle HAProxy logs. We need to create a new file with name /etc/rsyslog.d/99-haproxy.conf using vi editor or your preferred editor.

# vi /etc/rsyslog.d/99-haproxy.conf

Add following lines into the file:

$AddUnixListenSocket /var/lib/haproxy/dev/log

# Send HAProxy messages to a dedicated logfile
:programname, startswith, “haproxy” {

The $AddUnixListenSocket directive tells Rsyslog to create a Unix domain socket in the specified location, in this case /var/lib/haproxy/dev/log. The :programname, startswith, “haproxy” section specifies the file where Rsyslog will write the log entries to that it collects from the socket.

Save and exit.

We have successfully configured Rsyslog to read log entries from the Unix domain docket.

Before you restart rsyslog service, first check if the SELinux is enforcing. Run following command to verify it:

# getenforce

If SELinux is Enforcing mode, SELinux is access controls on our system. In that case, we will need to complete these steps: first before we move forward.

If SELinux is Permissive or Disabled mode, run following command to restart the rsyslog service:

# systemctl restart rsyslog

Now, you can view logs in the /var/log/haproxy.log file that you configured in /etc/rsyslog.d/99-haproxy.conf.

Step 5 – Test HAProxy Logging

By default the haproxy package ships with a configuration file that creates an HTTP listener socket on port 5000. The configuration points to a non-existent backend server, so any request to the port will result in an HTTP 503 error.

To check for a 503 error in your /var/log/haproxy.log file, first generate an HTTP request using curl like this:

# curl -si

You should receive output like the following:

HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.

Now, we can see the log file

# tail /var/log/haproxy.log

We have learnt how to install HAProxy logging with Rsyslog on CentOS 8 in this artcle.

Checkout our 10Gbps dedicated server plans.

Related Articles