Directive inheritance

Lucas Rolff lucas at lucasrolff.com
Fri Oct 6 19:32:51 UTC 2017


Hi guys,

I do a lot of nginx configuration which contains plenty of “location” blocks, however – I often see myself duplicating a lot of directives throughout my configuration which can sadly make a single nginx server block about 400 lines long, often due to repeated settings.

Not only is it a mess with big files (at least they’re generated automatically), but I also have the feeling I waste some memory if I keep redefining the settings again and again (or is nginx smart enough to “deduplicate” settings whenever possible?)

My configs usually look something like

server {

  location / {
     // sendfile, client_body_buffer_size, proxy_* settings, add_header repeated
   location  ~* \.(?:htm|html)$ {
       // sendfile, client_body_buffer_size, proxy_* settings, add_header repeated
    }
    location ~* \.(?:manifest|appcache|xml|json)$ {
       // sendfile, client_body_buffer_size, proxy_* settings, add_header repeated
   }
  }
}

I know that there’s some settings such as proxy_pass which can’t inherit from the parent location or server block, however – is there any semi-easy way to figure out if a directive in nginx or it’s modules gets inherited or not? (I don’t mind digging around in some nginx source code)

I could try to remove a bunch of directives from the lower location directives and see if things still work, however it would be very time consuming.

Reading the docs of nginx and it’s directive, *sometimes* the docs say whether a directive gets inherited or not, but it’s not always complete – such as sendfile for example, as far as I know it gets inherited, but it doesn’t say in the docs.

The directives I mostly use are things like:

proxy_*
Sendfile
Client_body_buffer_size
Add_header
Expires (these differs for each location block I have)

I wonder if someone either knows a good way to figure out, or any document on the web that goes extensively into explaining what (might) inherit based on general design patterns.

Also if anyone can either confirm or deny whether duplicating the directives actually increase memory usage – because if it has next to no additional resource usage – then I could save some time.

The amount of zones/server blocks are currently small, but I’d like to be able to scale it to thousands on fairly common hardware.

Best Regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20171006/2070d12b/attachment.html>


More information about the nginx mailing list