3

I'm looking to extend the mime types in my Nginx configuration.

I've learned that I could, in principle, either edit the mime.types file, or after including mime.types in the http block of the config you could follow include mime.types with a types = {...} to append more types a la this answer.

Since I'm setting up Nginx with Chef, I have a templated configuration in sites-enabled folder that's included into the Nginx config proper. I would prefer not to have to template the nginx config or mime.types file, so I'm hoping it's possible to get it in the sites-enabled config file.

In a similar spirit to the linked question above, could I include this in my sites-enabled file to get the same effect?

http {
    types {
        # here is additional types
    }
}

My working theory is that if blocks work as described in the link above, adding such a block would not overwrite the http block in the Nginx config, but would extend it as if I had added the types directly to the http block in nginx.conf.

Is this a valid strategy? Or am I overlooking something easier?

Community
  • 1
  • 1
rschwieb
  • 746
  • 2
  • 16
  • 41

1 Answers1

6

Although not explicitly stated in the documentation, the nginx types directive appears to behave similarly to other directives with regard to inheritance.

The directive is inherited from the previous level if and only if there are no type directives defined on the current level.

The types directive may appear in the http, server or location block level.

To extend the mime types (rather than redefining them) to can add a types block with your additions in any file containing an http, server or location context.

But if you add a types block at the server or location level, you should also add another include mime.types statement at the same level, so as not to lose the system defaults.

In your sites-enabled files:

# (1)
server {
    # (2)
    location ... {
        # (3)
    }
}

If your sites-enabled file includes the server { ... } block definition, you might add a types block in position (1) which would augment the mime types already loaded by the main nginx.conf file.

However, if you added a types block in positions (2) or (3), you would also need to add an include statement to pull in the system types again:

server {
    include mime.types;
    types { ... }  
    ...
}

The types directive is documented here.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • With my nginx 1.18.0 on Ubuntu 20.04 setup, putting `types` in `location` always invalidate the system defaults, regardless of whether `include mime.types` is specified or not. But putting custom `types` in `server` does successfully combine `mime.types` and my additional `types` properly. – wlnirvana Aug 25 '21 at 01:08