custom log_format not inherited by server block

Maxim Dounin mdounin at mdounin.ru
Fri May 18 14:27:22 UTC 2018


Hello!

On Thu, May 17, 2018 at 11:03:08PM +0100, Pete Cooper wrote:

> I am compiling Nginx 1.14.0 from source on Ubuntu 18.04 LTS with 
> a view to compiling ipscrub as a dynamic module. 
> 
> My compile completes without error, my nginx.conf validates, 
> Nginx runs as expected, yet my server block throws an error 
> about an unknown log format. 
> 
> If my `log_format` directive appears after the `access_log` 
> directive in nginx.conf, it will not validate, stating: 
> 
>     nginx: [emerg] unknown log format "ipscrubbed" in /etc/nginx/nginx.conf:15 
> 
> If my `log_format` directive appears before the `access_log` 
> directive in nginx.conf, it validates. 

That's expected.  The list of formats is global, and nginx will 
lookup appropriate format when processing the "access_log" 
directive.  As such, you have to define log_format before access_log 
which uses it.

> If my `log_format` directive appears before the `access_log` 
> directive in nginx.conf, the default server block will not 
> validate, stating: 
> 
>     nginx: [emerg] unknown log format "ipscrubbed" in /etc/nginx/sites-enabled/default:2 
> 
> …implying that although my custom `log_format` is valid, the 
> default server block is not inheriting it. Which has completely 
> thrown me. Do I need to reposition the `access_log` directive to 
> a later point in the server block? Or is there something else 
> fundamental that I'm overlooking? 

Quoting the nginx.conf file:

      include /etc/nginx/sites-enabled/*;
      keepalive_timeout 65;
      log_format ipscrubbed '$remote_addr_ipscrub';

That is, the "ipscrubbed" format is defined _after_ specific server{} 
blocks are included from sites-enabled.  With such a configuration 
you won't be able to use the "ipscrubbed" format in these included 
configuration files.

To fix things, consider defining log_format _before_ including 
server-specific configuration files.  The best solution would be 
to move the

      include /etc/nginx/sites-enabled/*;

line to the end of the http{} block.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list