Nginx:如何复制Apache的other_vhosts_access.log文件以在日志行开始时获取虚拟主机?

我在运行Nginx的服务器上托pipe了多个站点。 我不想为所有虚拟主机使用单独的日志文件,但是我确实希望能够从日志中看出每个请求所属的Nginx虚拟主机。 这对于/var/log/nginx/access.log使用的默认NCSA“组合”格式是不可能的。

Debian / Ubuntu下的Apache默认login到/var/log/apache2/other_vhosts_access.log,其中包括虚拟主机名。 我如何复制这个Nginx的?

将下面的代码片段保存为/etc/nginx/other-vhosts-access-log.conf并重新加载Nginx。 这将开始logging到/var/log/nginx/other_vhosts_access.log。 (这不会停止logging到/var/log/nginx/access.log。)格式是相同的(NCSA“组合”格式),但是在行的开始处具有server_name的值。 请注意,使用虚拟主机的名称,这不一定与请求的主机名相同(如果有其他的server_name指令用于别名)。

 # borrowed from Apache # (Could use $host instead of $server_name to log vhost aliases separately) log_format vhost_combined '$server_name $remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; # Define an access log for VirtualHosts that don't define their own logfile access_log /var/log/nginx/other_vhosts_access.log vhost_combined 

(注意日志轮转,但是这应该由nginx-full软件包的logrotate文件覆盖/ var / log / nginx-full文件。)

警告 :不要尝试通过使用access_log off;禁用日志logging到/var/log/nginx/access.log access_log off; 之前,因为这将阻止后续access_log语句工作。