#+TITLE: Fixing OpenBSD 6.7 httpd MIME Types #+DATE: 2020-08-15T01:49:22-04:00 #+DRAFT: false #+DESCRIPTION: How to get OpenBSD httpd to return the proper MIME types for files #+TAGS[]: openbsd httpd #+KEYWORDS[]: openbsd httpd #+SLUG: #+SUMMARY: #+ATTR_HTML: :title OpenBSD httpd MIME type recognition #+ATTR_HTML: :alt OpenBSD httpd MIME type recognition [[file:cover.png]] On OpenBSD's httpd, there are only a select few MIME types that are recognized by default. According to [[https://man.openbsd.org/httpd.conf.5][=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 [[https://man.openbsd.org/httpd.conf.5#TYPES][=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 ...]=. #+begin_src 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 } #+end_src 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 [[https://man.openbsd.org/httpd.conf.5#include][=include=]] directive. OpenBSD's MIME type definitions are stored in =/usr/share/misc/mime.types=. #+BEGIN_SRC types { include "/usr/share/misc/mime.types" } #+END_SRC After editing the configuration file, all you have to do is reload =httpd=, and it should begin recognizing file types properly. #+BEGIN_SRC shell doas rcctl reload httpd #+END_SRC