OpenBSD httpd MIME type recognition

On OpenBSD's httpd, there are only a select few MIME types that are recognized by default. According to httpd.conf(5), those types are: ext/css, text/html, text/plain, image/gif, image/png, image/jpeg, image/svg+xml, and application/javascript. Everything else is said to be of type application/octet-stream by default.

This is OK for most static hosting situations, but can be challenging for some common attachment types. For example, I recently made a blog post that had an attached PDF. Normally web browsers open PDF files in the browser's PDF viewer, but since it was being returned with the MIME type application/octet-stream, the browser decided to download it instead of opening it directly. This isn't a tragedy, but it can be annoying and interrupt the flow of the user's experience.

The solution to this is to include the application/pdf MIME type in a TYPES block in your httpd.conf. There are two ways to go about this. Either by manually defining the type you require, or including the system's built-in MIME type database.

Using the first method you would include a types block that defines all of the MIME types you require. This types block can go anywhere outside of a server declaration.

The types are defined in the format of type/subtype name [name ...].

types {
    text/css                css
    text/html               html htm
    text/plain              txt
    image/gif               gif
    image/jpeg              jpeg jpg
    image/png               png
    application/javascript  js
    application/pdf         pdf
}

The second method, including the system's built-in MIME types database, is simpler if you need to support proper MIME types for a larger number of files, and don't want to go through the process of writing them all in by hand. This is done by defining a similar types block, but instead of writing in type data, you use the include directive. OpenBSD's MIME type definitions are stored in /usr/share/misc/mime.types.

types {
    include "/usr/share/misc/mime.types"
}

After editing the configuration file, all you have to do is reload httpd, and it should begin recognizing file types properly.

doas rcctl reload httpd