How to Add a Single MIME Type in Nginx Without Overriding Existing Types


4 views

When attempting to add Markdown rendering support in Nginx by adding the following configuration:

types {
    text/markdown md;
}

Many developers encounter an unexpected side effect - the server stops rendering standard files like index.html properly. This happens because the types directive in Nginx completely replaces the default MIME type mappings rather than extending them.

Nginx loads its default MIME types from the mime.types file (typically located in /etc/nginx/mime.types). When you specify a new types block in your server configuration, it doesn't merge with the defaults but instead overrides them completely.

To add just the Markdown MIME type while preserving all existing mappings, you have two main options:

Option 1: Include Default Types Explicitly

types {
    text/html               html htm shtml;
    text/css                css;
    text/xml                xml;
    image/gif               gif;
    image/jpeg              jpeg jpg;
    application/javascript  js;
    application/atom+xml    atom;
    application/rss+xml     rss;
    
    # Your custom addition
    text/markdown           md;
}

Option 2: Include the Default mime.types File

include /etc/nginx/mime.types;
types {
    text/markdown md;
}

For better maintainability, consider adding Markdown support only where needed:

location ~* \.md$ {
    types {}
    types { text/markdown md; }
    default_type text/markdown;
}

After making changes, always test with:

sudo nginx -t
sudo systemctl reload nginx

Then verify the MIME type is correctly served using curl:

curl -I http://yourserver.com/test.md

When attempting to add Markdown support in Nginx by adding the following directive inside the server block:

types {
    text/markdown md;
}

Many developers encounter an unexpected side effect: default MIME type mappings (like text/html for .html files) stop working. This happens because the types directive completely replaces Nginx's default MIME type mappings rather than merging with them.

The correct approach is to explicitly include Nginx's default types while adding your custom mapping. Here's the proper implementation:

types {
    text/markdown md;
    include /etc/nginx/mime.types;
}

This maintains all default MIME type mappings while adding your custom Markdown mapping.

For more granular control, you can add MIME types at the location level:

location ~* \.md$ {
    types { }
    default_type text/markdown;
}

This ensures only Markdown files receive the special treatment while leaving other files untouched.

After making changes, always test your configuration:

nginx -t
systemctl reload nginx

Then verify using curl:

curl -I http://yourserver/test.md

You should see Content-Type: text/markdown in the response headers.

1. The path to mime.types may vary depending on your Nginx installation
2. Multiple types blocks in the same context will override each other
3. For performance, consider placing frequently-used MIME types first