Haproxy 不记录请求?

Sil*_*ier 15 logging haproxy rsyslog

因此,我配置了 Haproxy,因此日志记录将通过 rsyslog,并且现在全部转储到一个文件中。

它肯定会记录日志,因为我在启动时收到那些“开始”消息,但根本没有记录 HTTP 请求。我的配置有什么问题?

haproxy.cfg :

global
        log /dev/log local0 debug
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend webfront
  option  forwardfor
  stats enable
  stats uri /haproxy?statis
  stats realm Haproxy\ Auth
  stats auth user:password
  bind *:80
  timeout client 86400000
  acl is_discourse  hdr_end(host) -i discourse.mydomain.com
  use_backend       discourse     if is_discourse
  use_backend       webserver     if !is_discourse

backend discourse
  balance source
  option forwardfor
  option httpclose
  server server1 127.0.0.1:3080 weight 1 maxconn 1024 check inter 10000

backend webserver
  balance source
  option forwardfor
  option httpclose
  server server2 127.0.0.1:4080 weight 1 maxconn 1024 check inter 10000
Run Code Online (Sandbox Code Playgroud)

日志文件

root@kayak:/var/log/haproxy# tail haproxy.log
Nov 26 21:25:25 kayak haproxy[21646]: Proxy webfront started.
Nov 26 21:25:25 kayak haproxy[21646]: Proxy webfront started.
Nov 26 21:25:25 kayak haproxy[21646]: Proxy discourse started.
Nov 26 21:25:25 kayak haproxy[21646]: Proxy webserver started.
Nov 26 21:28:10 kayak haproxy[21868]: Proxy webfront started.
Nov 26 21:28:10 kayak haproxy[21868]: Proxy discourse started.
Nov 26 21:28:10 kayak haproxy[21868]: Proxy webserver started.
Nov 26 21:30:31 kayak haproxy[22045]: Proxy webfront started.
Nov 26 21:30:31 kayak haproxy[22045]: Proxy discourse started.
Nov 26 21:30:31 kayak haproxy[22045]: Proxy webserver started.
Run Code Online (Sandbox Code Playgroud)

我在重新启动之间访问了一些网络服务器页面并触发了一些 404 错误。为什么什么都不显示?

编辑:rsyslog conf 文件。

/etc/rsyslog.d/49-haproxy.conf:

local0.* -/var/log/haproxy_0.log
if ($programname == 'haproxy') then -/var/log/haproxy/haproxy.log
& ~
Run Code Online (Sandbox Code Playgroud)

edl*_*erd 13

如果您真的希望记录每个请求,则必须在前端指定日志。但通常这对服务器来说太过分了,你的磁盘很快就会满了。

frontend webfront
  log /dev/log local0 debug
Run Code Online (Sandbox Code Playgroud)

  • “defaults”部分中的“log global”行不是已经做到了这一点吗? (3认同)
  • 我尝试添加它,但它没有记录任何额外的内容。也许`rsyslog` 没有正确配置?有办法告诉吗? (2认同)
  • 什么是`/dev/log` (2认同)

小智 8

在我的 rhel 6.7 上,通过 unix socket log 的日志记录对我不起作用。你可以试试这个 conf。haproxy(在 81 上工作)将 http 请求转发到 httpd(在 80 上工作)

/etc/haproxy/haproxy.cfg

frontend web_front
    log         127.0.0.1    local6
    option httplog

    bind        *:81
    default_backend web_back

backend web_back
    server      web1 127.0.0.1:80
Run Code Online (Sandbox Code Playgroud)

并且您必须启用 rsyslog udp 模块以从 haproxy 接收 syslog 一个像这样的简单配置:

/etc/rsyslog.d/haproxy.conf

$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514
local6.* /var/log/haproxy.log
Run Code Online (Sandbox Code Playgroud)

向 81 发出一个 http 请求,你会得到一些这样的日志

# tail -n 1 /var/log/haproxy.log
May 18 13:51:07 localhost haproxy[31617]: 127.0.0.1:38074 [18/May/2016:13:51:06.999] web_front web_back/web1 0/0/0/2/2 404 466 - - ---- 1/1/0/1/0 0/0 "GET /how-are-you HTTP/1.1"
Run Code Online (Sandbox Code Playgroud)


car*_*rla 7

这个链接完美地解释了它。

如果您查看 /etc/haproxy/haproxy.cfg 的顶部,您会看到如下内容:

global
log 127.0.0.1 local2
[...]

这意味着 HAProxy 会将其消息发送到 127.0.0.1 上的 rsyslog。但默认情况下,rsyslog 不侦听任何地址,因此存在问题。

让我们编辑 /etc/rsyslog.conf 并取消注释这些行:
$ModLoad imudp
$UDPServerRun 514

这将使 rsyslog 侦听所有 IP 地址的 UDP 端口 514。(可选)您可以通过添加以下内容来限制为 127.0.0.1:
$UDPServerAddress 127.0.0.1

现在创建一个 /etc/rsyslog.d/haproxy.conf 文件,其中包含:

local2.* /log/haproxy.log

您当然可以更具体并根据消息级别创建单独的日志文件:

local2.=info /log/haproxy-info.log
local2.notice /log/haproxy-allbutinfo.log

然后重启rsyslog,看到创建了日志文件:
# service rsyslog restart

如果您手动创建日志文件/log/haproxy-allbutinfo.log/log/haproxy-info.log,请不要忘记将所有者更改为syslog:adm


小智 6

这可能是由于它在 chroot jail 中运行造成的。您需要确保 rsyslog 也在 chroot jail 中创建了一个 dgram 套接字(例如 /var/lib/haproxy/dev/log)。将您的日志指令指向 /dev/log 套接字,您应该没问题。

我花了几个小时试图解决这个问题,因为 HAproxy 不会告诉你除了登录失败之外有什么问题。