每个用户的虚拟主机日志记录

每个用户的虚拟主机日志记录

我有一个有效的 Apache 每个用户虚拟主机配置,但我希望每个用户都能访问其虚拟主机的日志。显然,ErrorLog 和 CustomLog 指令不接受 VirtualDocumentRoot 所接受的通配符语法,但有没有办法在每个用户的目录中实现日志?

<VirtualHost *:80>
    ServerName *.example.com
    ServerAdmin [email protected]
    VirtualDocumentRoot /home/%2/projects/%1
    <Directory /home/*/projects/>
        Options FollowSymlinks Indexes
        IndexOptions FancyIndexing FoldersFirst
        AllowOverride All
        Order Allow,Deny
        Allow From All
        Satisfy Any
    </Directory>

    Alias /favicon.ico /var/www/default/favicon.ico
    Alias /robots.txt /var/www/default/robots.txt

    LogLevel warn
    # ErrorLog /home/%2/logs/%1.error.log
    # CustomLog /home/%2/logs/%1.access.log combined
</VirtualHost>

解析度:基于@cjc 的精彩回答我意识到我需要编写一些脚本来解决这个问题,所以我想到了以下方法:

#!/bin/bash

##
# Write whatever comes through stdin to the log directory in each user's home directory.
##

# The first field in the incoming log must be the vhost name
# (%V in the Apache LogFormat).
while read vhost fields; do
        # vhost names take the form $project.$user.$host.         
        project="${vhost%%.*}" # Strip off everything after the first dotted name.
        user="${vhost#*.}" # Strip off the first dotted name.
        user="${user%%.*}" # Strip off everything but the first (remaining) dotted name.
        printf -v cmd "mkdir -p '/home/%s/log' && printf '%%s\n' '%s' >> '/home/%s/log/%s.log'" "$user" "$fields" "$user" "$project"
        sudo -u "$user" -- bash -c "$cmd"
done

答案1

我不相信您可以在 Apache 内部完成此操作,但您可以将日志传输到可以按照您尝试的方式进行拆分的脚本:

看看 Apache 文档的这一部分:

http://httpd.apache.org/docs/2.2/logs.html#piped

因此你将得到如下一行:

CustomLog "|/usr/local/bin/per-user-web-logs.pl" combined

例如,其中/usr/local/bin/per-user-web-logs.pl有一个 Perl 脚本,它将附加到正确的每个用户日志中。

相关内容