Chapter 6
[ 119 ]
Logging
NGINX has a very flexible logging model. Each level of configuration may have
an access log. In addition, more than one access log may be specified per level,
each with a different log_format. The log_format directive allows you to specify
exactly what will be logged, and needs to be defined within the http section.
The path to the log file itself may contain variables, so that you can build a dynamic
configuration. The following example describes how this can be put into practice:
http {
log_format vhost '$host $remote_addr - $remote_user [$time_local]
'
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
log_format downloads '$time_iso8601 $host $remote_addr '
'"$request" $status $body_bytes_sent $request_
time';
open_log_file_cache max=1000 inactive=60s;
access_log logs/access.log;
server {
server_name ~^(www\.)?(.+)$;
access_log logs/combined.log vhost;
access_log logs/$2/access.log;
location /downloads {
access_log logs/downloads.log downloads;
}
}
}