Forum Moderators: phranque
I run a cluster of 6 apache servers that share a common httpd.conf (through nfs). The problem I'm facing is with the log files.
I do not want each server to store the log file on their own hard drives because of space restrictions, and I instead require the log files to be on the NFS mount (located at /array/).
All six servers are writing to the log files at the same time, causing spliced lines and nearly unreadable output. My question is regarding this line:
CustomLog /array/log/httpd-access.log combined
Is there any way to save the log file to a dynamic path, such as:
CustomLog /array/log/__$LOCAL_HOST_NAME$__/httpd-access.log
Or even better, does anyone have any ideas on how to keep a single log file for all servers, without splicing and mutilating the output, and without slowing down each server, waiting for locks on the file to release?
I look forward to your help. Thanks in advance. :)
Or, I suppose you could pipe the log-lines through a script and have it enforce "per-record" locking, but beware of deadlocks caused by individual server crashes and by failed or flaky "flocks." Use timeouts to break these deadlocks after a fairly short period of time (milliseconds, not seconds) -- It's better to have "splices" than to lock up your logging process, for even the surviving servers!
Jim
I found your question on google looking for the same answer: clustered apache logs that automatically select per-host filenames without per-host config files. Based on that objective and on what jdMorgan mentioned above, I remembered seeing something some time ago relatve to this, and here's what I came up with:
1) Keep an httpd.conf file per host server. It can have a minimal number of directives in it and include external shared files that are common to all your hosts, but start with a unique file per host.
2) Inside httpd.conf per host, include the following directive:
# On server 1:
SetEnv www1 www1
# On server 2:
SetEnv www2 www2
# and so on for each host...
3) In your vhost container where the CustomLog directives live, create a CustomLog config line that matches each host depending on these environment variables:
# Per vhost, add both lines:
CustomLog "¦ /usr/local/apache/bin/rotatelogs /logs/www1_access_log.%Y%m%d 86400 -420" combined env=www1
CustomLog "¦ /usr/local/apache/bin/rotatelogs /logs/www2_access_log.%Y%m%d 86400 -420" combined env=www2
If there are 6 hosts, each vhost will have six such lines. Each line logs data to a different log file depending on which host has served the request based on the environment variable checking.
That's it, and works for me on latest version of Apache 2.2 sitting on Centos Linux, placing the logs all the the same shared NFS mounted directory.
Regards,
SK
Pay close attention to your operating system's file-locking semantics in order to avoid 'spliced' log entries. It shouldn't be necessary, since writing a record is normally an 'atomic' function, but if you are having trouble try using the 'flock' function to lock the log file while any given process or thread is appending a new record.
Jim