Docker-nginx: Seeking guidance for overriding log_format of default access log

Created on 22 Aug 2015  路  5Comments  路  Source: nginxinc/docker-nginx

An issue has come up over at https://github.com/jwilder/nginx-proxy/pull/216 regarding overriding the log_format for the main access_log defined in nginx.conf.

In https://github.com/jwilder/nginx-proxy/pull/62, I added a log_format and an access_log directive in order to get the server name for the requested virtual host in the logs. I didn't notice it at the time, but it turns out that this created duplicate access log lines on STDOUT.

I'm suspecting that the problem is that the access_log in nginx.conf is writing to the /var/log/nginx/access.log symlink while the one I added to nginx-proxy's generated default.conf file refers to /proc/self/fd/1.

My question here is: what is the recommended way to override the log_format for the main log defined in nginx.conf? Another customization that @jgallen23 expressed a need for was the ability to change the access_log to a JSON-style output using a custom log_format.

Most helpful comment

Thanks @thresheek.

I had seen that note in the docs about access_level off canceling all access_log directives at the current level, but I had assumed it meant all _previous declared_ access_log directives and that it should not affect anything subsequent to the access_log off directive at the same level.

It looks like the trick is to set access_log off at the http level and turn it back on at a lower level such as inside the server block.

After testing with that configuration, it does indeed look like things work fine. :+1:

All 5 comments

I just did a bit more testing, so I thought I'd report my results.

First, I tried just changing the access_log statement that's added in jwilder/nginx-proxy's default.conf to point to the same filename as the nginx.conf in this image. The result was that it seemed to be ignored (I saw only the default logging without a virtual host name). See https://github.com/appropriate/nginx-proxy/commit/42cd700acb44881305dc626e978f553c5bb4cae3

Next, I tried using access_log off to disable the logging from nginx.conf before overriding it, but that resulted in access logging being disabled. See https://github.com/appropriate/nginx-proxy/commit/ab53361eafcb366dc96ec52c91f7daefd12586a7

have you tried setting

access_log /var/log/nginx/access.log vhost;
error_log /var/log/nginx/error.log;

in https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl#L38 ?

@thomasleveil Yes. My comment perhaps wasn't clear enough, but that's the change I mentioned having tested in https://github.com/appropriate/nginx-proxy/commit/42cd700acb44881305dc626e978f553c5bb4cae3

When I made that change, I only saw the original log line as defined by the main format in nginx.conf. I don't have error_log in nginx.tmpl in that commit, but I don't think that should make a difference.

If you are providing the template to be included in the conf.d you can use something similar to https://gist.github.com/thresheek/c2cd242f78abc50f3294.

setting access_log off; and then redefining it on the same level is explicitely mentioned as a no-go in the docs, http://nginx.org/r/access_log, "The special value off cancels all access_log directives on the current level".

Another option (albeit I can not recommend it enough) would be to ship a custom top-level nginx.conf that suits your needs.

Thanks @thresheek.

I had seen that note in the docs about access_level off canceling all access_log directives at the current level, but I had assumed it meant all _previous declared_ access_log directives and that it should not affect anything subsequent to the access_log off directive at the same level.

It looks like the trick is to set access_log off at the http level and turn it back on at a lower level such as inside the server block.

After testing with that configuration, it does indeed look like things work fine. :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rokf picture rokf  路  5Comments

sandersaares picture sandersaares  路  6Comments

colinmollenhour picture colinmollenhour  路  6Comments

lucacome picture lucacome  路  3Comments

eigood picture eigood  路  5Comments