覆盖nginx的access_log指令 – 重复的日志条目

我在Ubuntu 14.04服务器上使用默认的nginx包。 它使用/etc/nginx/nginx.conf作为主configuration,然后包含/etc/nginx/conf.d/*.conf/etc/nginx/sites-enabled/*

默认的nginxconfiguration有这个指令来logging到访问日志

access_log /var/log/nginx/access.log;

我想添加X-Forwarded-For标题,所以我在conf.d文件夹中执行此操作:

 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; 

我的问题是,然后我得到两个logging在我的access.log文件 – 一个与标题信息和另一个没有。

我知道我可以覆盖nginx.conf文件本身,但我宁愿避免它,如果可能的话。 我也想继续使用相同的日志文件( access.log )。

有没有办法告诉nginx覆盖以前的指令,只是改变日志格式而不修改主nginx.conf文件?

日志格式的定义需要在http部分

 http { 

 log_format main'$ remote_addr  -  $ remote_user [$ time_local]'
                 '“$ request”$ status $ body_bytes_sent“$ http_referer”'
                 '“$ http_user_agent”“$ http_x_forwarded_for”';

 } 

我已经注意到很多次了,nginx并没有像我们想要的那样行为,只是因为它不会因为错误的地方而使我们的configuration生效,也不会抛出错误。