Virtual host log format
If you're running a virtual host style environment (with multiple server
blocks) with NGINX, there's one small tweak you can make to enhance the logs. By default, the host (defined as $host
) isn't logged when using the default combined format. Having this field in the logs means that the log can be parsed externally without the need for additional information.
How to do it...
To start with, we need to define our new log format:
log_format vhostlogs '$host $remote_addr - $remote_user ' '[$time_local] "$request" $status ' '$body_bytes_sent "$http_referer" ' '"$http_user_agent"';
Note
The log_format
directive needs to be outside the server
block.
To use our new log file, we update the access log format in the server
block with the new format. Here's an example with our MediaWiki recipe:
access_log /var/log/nginx/mediawiki-vhost-access.log vhostlogs;
If you had a wildcard setup for your subdomains, this would...