大网数据广告活动
服务器运维问答

Dynamic Error and Custom logs in apache 2 using wildcard subdomains

· 52CE 我爱测

I'm trying to make a wildcard VirtualHost conf file for apache2, and I'm not sure how to handle the ErrorLog and CustomLog settings to put the logs where I want them to go. As you can see in the second VirtualHost , I have the lo…

问题原文

原帖链接 · Mr. Simmons · CC BY-SA 4.0

I'm trying to make a wildcard VirtualHost conf file for apache2, and I'm not sure how to handle the ErrorLog and CustomLog settings to put the logs where I want them to go. As you can see in the second VirtualHost, I have the logs in a logs folder in the domains DocumentRoot. This works fine for static VirtualHosts, but how would I go about it for a wildcard VirtualHost. eg, the first VirtualHost.

NameVirtualHost *:80

# Wild card all subdomains
<VirtualHost *:80>
        ServerAlias *.example.com
        VirtualDocumentRoot /var/www/%0/public
        ErrorLog ?????
        CustomLog ????? combined
</VirtualHost>


# Main domain
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public
        ErrorLog /var/www/example.com/logs/error.log
        CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

I have tried going ErrorLog /var/www/*.example.com/logs/error.log and ErrorLog /var/www/%0/logs/error.log and the same for CustomLog, but when I try to restart apache it throws an error.

What syntax should I use to get a working version of my ErrorLog example above?

I have seen Wildcards in Virtual Hosts with dynamic logs? , but it is not really what I am after as it still ends up putting all the logs into one big file rather than having them split into their own subdomain specific folders.

已采纳解答

原帖链接 · oucil · CC BY-SA 4.0

At the risk of giving you more work, you may also want to consider going the opposite direction and combining ALL of your logfiles into syslog. It seems more intimidating but it's a much more capable solution. I don't bother with separate logs anymore at all and just pipe all logs directly to the syslog (which keeps everything in /var/log/messages).

Though Apache doesn't do it natively, you can achieve it like so...

LogLevel info
ErrorLog  "| /usr/bin/logger -thttpd -plocal6.err"
CustomLog "| /usr/bin/logger -thttpd -plocal6.notice" "%v %h %l %u %t \"%r\" %>s %b"

Syslog will give you a lot of options as there are tons of services and utilities set up to parse and manage it. I love using lnav personally, you can filter, sort, search, etc. and the interface over ssh is colourized so it's easy to spot problems. You can also use utilities to deliver all of this into a SQL db where the sky's the limit, or for less work you can use commercial services like datadog that have pretty dashboards :).

You'll want to read up on things like facilities (local1-7) which allow you to assign groups basically to services that don't have syslog logging built in.

In the definition above, I'm piping both my access and error logs to logger which is a utility designed to accept logs and record them in a common format in the syslog along with the details of the service providing them. You can see that the %v is the first part of my access log which makes it easy to filter for vhosts in the syslog down the road.

You'll notice in the directives, that the error log uses local6.err while the access log uses local6.notice, this actually sets the level of the log line, the errors will end up coloured red in lnav, while the others will be standard/info.

Beware the rabit hole ;)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。