Categories
Nginx Linux

How to Update Nginx MIME Types in 2 Steps

Nginx can recognize different content types through a predefined MIME type list. Your Nginx MIME types might be outdated if you install Nginx using your system package system, e.g., apt install nginx on Ubuntu/Debian or yum on CentOS.

Using apt or yum to install packages are very easy and, more importantly, you will get security updates throughout the release.

But when you inspect the Content-Type HTTP header of some files served by your Nginx installation, it is the incorrect but default application/octet-stream MIME type. And you might also want to cache certain MIME type such as the font type woff and woff2, which are only added to Nginx in 2018.

How do I update the list of mime types for my Nginx installation?

Get updated MIME types

First, we will need to find where is the latest version of the MIME types, which is the mime.types file.

The mime.type file is in the Nginx code repository. Fortunately, there is a repo on GitHub https://github.com/nginx/nginx, which is a mirror to Nginx’s Mercurial repo: http://hg.nginx.org/nginx/, which is hard to find files.

The mime.types file on Nginx GitHub repo
The mime.types file on Nginx GitHub repo

First, we use wget to download the file from Nginx’s GitHub repo:

wget https://github.com/nginx/nginx/raw/master/conf/mime.types

You should see the output like this:

--2020-04-20 11:07:38--  https://github.com/nginx/nginx/raw/master/conf/mime.types
Resolving github.com (github.com)... 140.82.114.3
Connecting to github.com (github.com)|140.82.114.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types [following]
--2020-04-20 11:07:38--  https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...                                                                                       
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5231 (5.1K) [text/plain]
Saving to: โ€˜mime.typesโ€™

mime.types                100%[====================================>]   5.11K  --.-KB/s    in 0.001s

2020-04-20 11:07:39 (5.56 MB/s) - โ€˜mime.typesโ€™ saved [5231/5231]

Overwrite the MIME types

The MIME types file mime.types in your Nginx installation is located in /etc/nginx/ directory. So we’ll copy the file that you just downloaded to this directory to overwrite the old one using cp:

sudo cp mime.types /etc/nginx/

If you don’t see any output, that means the file is overwritten.

Let’s double check by looking at the modified time of the file:

ls -l /etc/nginx/mime.types

You will see some output like the following. The modified time is in bold and it should be your current time.

-rw-r--r-- 1 root root 5231 Apr 20 11:09 /etc/nginx/mime.types

Reload Nginx

Now we have the latest MIME types. You can take a look using less /etc/nginx/mime.types and search for the MIME type that was missing.

We will need to let Nginx reload the config file to let it see the updated mime.types file:

sudo service nginx reload

If you don’t see any output, that means Nginx now loaded the updated MIME types.

To double check, open the webpage that the MIME type was missing and check the Content-Type HTTP header.

Alternatively, you can also use curl -I to check HTTP header of an URL. For example, to check lgtm.cf:

curl -I https://lgtm.cf/

In A Nutshell

  1. Download the latest mime.type file using wget:

    wget https://github.com/nginx/nginx/raw/master/conf/mime.types

  2. Copy to Nginx config directory

    sudo cp mime.types /etc/nginx/

  3. Reload Nginx

    sudo service nginx reload

Tools
  • Terminal
  • SSH

giphy

0

By VarHowto Editor

Welcome to VarHowto!