Home Linux Enable Logging in Haproxy

Enable Logging in Haproxy

HAProxy can emit log message for processing by a syslog server. This is compatible with familiar syslog tools like Rsyslog, as well as the newer systemd service journald. You can also utilize various log forwarders like Logstash and Fluentd to receive Syslog messages from HAProxy and ship them to a central log aggregator.

If you are working in a container environment, HAProxy supports Cloud Native Logging which allows you to send the log messages to stdout and stderr. In that case, skip to the next section where you’ll see how.

vi /etc/rsyslog.d/haproxy.conf
# Collect log with UDP
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514

# Creating separate log files based on the severity
local0.* /var/log/haproxy-traffic.log
local0.notice /var/log/haproxy-admin.log

Now in haproxy.cfg add log 127.0.0.1:514 local0 under global

vi /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1:514 local0

Save and restart haproxy

systemctl restart haproxy
[root@haproxy ~]# tail -f /var/log/haproxy-traffic.log
Jul  6 03:49:10 localhost haproxy[11182]: 176.31.20.253:43664 [06/Jul/2020:03:49:10.215] https~ app-main/nginx3 0/0/2/26/28 200 379 - - ---- 1/1/0/0/0 0/0 "POST /wp-cron.php?doing_wp_cron=1594021749.8861470222473144531250 HTTP/1.1"
Jul  6 03:49:10 localhost haproxy[11181]: 196.207.127.213:34788 [06/Jul/2020:03:49:09.067] https~ app-main/nginx1 0/0/5/1317/1322 200 7274 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul  6 03:49:15 localhost haproxy[11180]: 196.207.127.213:43138 [06/Jul/2020:03:49:15.301] https~ app-main/nginx1 0/0/5/247/252 200 8140 - - ---- 1/1/0/0/0 0/0 "GET /hello-world/ HTTP/1.1"
Jul  6 03:49:19 localhost haproxy[11182]: 196.207.127.213:48570 [06/Jul/2020:03:49:19.747] https~ app-main/nginx1 0/0/4/165/169 200 7274 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jul  6 03:49:28 localhost haproxy[11182]: 196.207.127.213:57796 [06/Jul/2020:03:49:26.572] https~ app-main/nginx2 0/0/1/1750/1751 200 5911 - - ---- 1/1/0/0/0 0/0 "GET /category/uncategorized/ HTTP/1.1"
Jul  6 03:49:31 localhost haproxy[11182]: 196.207.127.213:61352 [06/Jul/2020:03:49:29.382] https~ app-main/nginx3 1/0/1/1694/1696 200 5911 - - ---- 2/2/0/0/0 0/0 "GET /category/uncategorized/ HTTP/1.1"
Jul  6 03:49:34 localhost haproxy[11181]: 196.207.127.213:9496 [06/Jul/2020:03:49:32.901] https~ app-main/nginx2 0/0/1/1646/1647 200 7343 - - ---- 1/1/0/0/0 0/0 "GET /2020/06/ HTTP/1.1"

Must Read

Routing Domain Name in Haproxy

I hope you are following the below guide. Now suppose you want to route domain name to open specific backend that have...

Enable Logging in Haproxy

HAProxy can emit log message for processing by a syslog server. This is compatible with familiar syslog tools like Rsyslog, as well...

Getting real IP in Nginx when behind cloudflare Haproxy

Create a file named cloudflare_ips.ls and in that add all the cloudflare IP's vi /etc/haproxy/cloudflare_ips.ls 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22 141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20 197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/12 172.64.0.0/13 131.0.72.0/22 2400:cb00::/32 2606:4700::/32 2803:f800::/32 2405:b500::/32 2405:8100::/32 2a06:98c0::/29 2c0f:f248::/32

Getting Real IP in HaproxyNginx configuration

Well its a little configuration can get real to nginx when haproxy is set as reverse proxy. Follow this...

Haproxy as Single Point to Failure Node With Glusterfs and MariaDB Maxscale Cluster

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It...