Apache:每个虚拟主机自动拆分日志?

Apache:每个虚拟主机自动拆分日志?

每当我们在开发服务器上创建新的虚拟主机时,我们总是必须手动指定单独的访问和错误日​​志:

ErrorLog logs/mysite/dev1-error.log
CustomLog logs/mysite/dev1-access.log "common"

有没有什么方法可以让 Apache 自动拆分日志,而不必每次都指定它们?

答案1

我理解你的痛苦,作为一名 Web 开发人员,我的本地开发中可能有超过 200 个虚拟主机 - 我个人并不关心日志并将它们全部记录到主 /var/log/apache 中...

然而我所做的是编写一个 shell 脚本来添加/管理我的所有 vhost - 您需要做的就是对其进行调整以将日志写入您喜欢的任何位置...

#!/bin/bash
ARGS=1
if [ "$1"X = "X" ];
        then
        echo "Must enter domain name"
        exit 0
fi
if [ "$2"X = "X" ];
        then
        echo "Must enter domain suffix"
        exit 0
fi
if [ "$3"X = "X" ];
        then
        echo "you must type "restart" if you want apache restarted "no" if not!"
        exit 0
fi

domain=$1.$2;

#echo $domain;
#exit 0


rm $domain.conf

echo "<VirtualHost *:80>" >> $domain.conf;
echo "        ServerAdmin [email protected]" >> $domain.conf;
echo "        ServerName $1.network.local" >> $domain.conf;
echo "        DocumentRoot /Data/vhome/$1.$2/httpdocs" >> $domain.conf;
echo "        HostnameLookups Off" >> $domain.conf;
echo "        UseCanonicalName Off" >> $domain.conf;
echo "        ServerSignature On" >> $domain.conf;
echo "        ScriptAlias /cgi-bin/ "/Data/vhome/$1.$2/cgi-bin/"" >> $domain.conf;
echo "        ErrorLog /var/log/apache2/error_log" >> $domain.conf;
echo "        CustomLog /var/log/apache2/access_log combined" >> $domain.conf;
echo "    <Directory "/Data/vhome/$1.$2/cgi-bin">" >> $domain.conf;
echo "        AllowOverride All" >> $domain.conf;
echo "        Options +ExecCGI -Includes" >> $domain.conf;
echo "        Order allow,deny" >> $domain.conf;
echo "        Allow from all" >> $domain.conf;
echo "    </Directory>" >> $domain.conf;
echo "    <Directory "/Data/vhome/$1.$2/httpdocs">" >> $domain.conf;
echo "        Options Indexes FollowSymLinks" >> $domain.conf;
echo "        AllowOverride All" >> $domain.conf;
echo "        Order allow,deny" >> $domain.conf;
echo "        Allow from all" >> $domain.conf;
echo "    </Directory>" >> $domain.conf;
echo "       # #XSS prevention" >> $domain.conf;
echo "       # RewriteEngine On" >> $domain.conf;
echo "       # RewriteCond %(REQUEST_METHOD) ^TRACE" >> $domain.conf;
echo "       # RewriteRule .* -[F]" >> $domain.conf;
echo "</VirtualHost>" >> $domain.conf;

if [ "$3" = "restart" ];
        then
        rcapache2 restart;
fi

chmod 666 $domain.conf

cat $domain.conf
echo "Created!";

exit 0

希望能帮助到你。

-肖恩

答案2

当我还在使用 Apache 时,我从一位同事那里学到了一个技巧。他通过一个脚本传输全局访问日志。然后awk该脚本会负责创建不同的日志文件。awk

在 httpd.conf 中:

LogFormat "%V %p %a %l %u %t \"%r\" %s %b \"%200{Referer}i\" \"%200{User-agent}i\" \"%{cookie}n\"" awklogpipe
CustomLog "|/usr/local/bin/apacheawklogpipe" awklogpipe env=!dontlog

脚本 /usr/local/bin/apacheawklogpipe:

#!/bin/gawk -f

BEGIN {
        months["Jan"] = "01";
        months["Feb"] = "02";
        months["Mar"] = "03";
        months["Apr"] = "04";
        months["May"] = "05";
        months["Jun"] = "06";
        months["Jul"] = "07";
        months["Aug"] = "08";
        months["Sep"] = "09";
        months["Oct"] = "10";
        months["Nov"] = "11";
        months["Dec"] = "12";
}

{
        # HEADS UP: SET THIS!!!!
        LOGBASE="/var/log/httpd/access"

        SSL=""

        # Automagicly set first 
        SITE=tolower($1);
        PORT=$2

        if ($2 == 443)
        {
                SSL="-ssl"
        }

        # Extract all but first two fields (vhostname vhostport to be exactly) into LINE
        LINE=substr($0,index($0,$3));

        # No matter where it is, we will find an apache datestamp.
        match(LINE,/\[[0-9]+\/[A-Z][a-z]+\/[0-9]+:[0-9]+:[0-9]+:[0-9]+[\t ]+[+-][0-9]+\]/);
        split(substr(LINE,RSTART + 1,RLENGTH - 2), ap_log_time, /[\/: ]/);

        #ap_rotatelog= LOGBASE "/" SITE ":" PORT "/access-log" SSL "-" ap_log_time[3] "-" months[ap_log_time[2]] ap_log_time[1];
        ap_rotatelog= LOGBASE "/" SITE  "/access_log" SSL;
        if (system("test -d " LOGBASE "/" SITE ) == 0)
        {
                print LINE >> ap_rotatelog;
                close(ap_rotatelog);
        }
        else
        {
                print SITE "\t" SSL "\t" LINE >> LOGBASE "/w3logrotate-error.log";
                close(LOGBASE "/w3logrotate-error.log");
        }
}

确保 /usr/local/bin/apacheawklogpipe 是可执行的。使用此脚本,您需要做的就是创建一个/var/log/httpd/access与虚拟主机名相对应的目录。我有一个脚本可以创建虚拟主机配置并创建日志目录。

答案3

另一个选择。不要在 httpd.conf 中拆分它们。相反,将所有内容记录到主访问日志中,然后稍后使用类似以下程序拆分它们分割日志文件.这有助于简化您的日志配置。

这描述于http://httpd.apache.org/docs/2.2/logs.html#virtualhost

通过将虚拟主机的信息添加到日志格式字符串中,可以将所有主机记录到同一个日志中,然后将日志拆分为单独的文件。例如,考虑以下指令。

LogFormat "%v %l %u %t \"%r\" %>s %b" comonvhost CustomLog
logs/access_log comonvhost

%v 用于记录为请求提供服务的虚拟主机的名称。然后像分割日志文件可用于对访问日志进行后处理,以便将其拆分为每个虚拟主机一个文件。

答案4

也许看看mod_macro。这是一种更通用的方法,但我想这也可以为您提供帮助。

相关内容