1 minute read

Nginx comes with a rather small set of mime types compared to a default Linux system

Linux uses a glob pattern to match a filename while Nginx matches only extension, but we can still use every glob in the format of *.ext

So here is a small PHP script converting/sorting/filtering/formatting everything in a nice output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// convert-mime-globs-to-nginx.php
!/usr/bin/env php
<?php
/**
 * Parses mime-type globs, as listed in /usr/share/mime/globs
 * Only parses group/id:*.extension
 * Usage: cat /usr/share/mime/globs | convert-mime-globs-to-nginx.php > /etc/nginx/mime.types
 */

$stdin = fopen('php://stdin', 'r');

if (!$stdin) {
  file_put_contents('Cannot open stdin', 'php://stderr');
  exit(1);
}

$mimes = array();
$extensions = array();
$mime_part = '[a-z0-9_\-\.]+';

while ($line = trim(fgets($stdin))) {
  if (preg_match($m = "!^($mime_part)/($mime_part)\s*:\*\.($mime_part)$!i", $line, $matches)) {
    // Make sure we have only one id per extension, case insensitive 
    $ext = strtolower($matches[3]);
    if (isset($extensions[$ext])) {
      file_put_contents("$ext cannot be registered as ${matches[1]}/${matches[2]}, it is already registered as ${extensions[$ext]}\n", 'php://stderr');
    } else {
      $mimes[$matches[1]][$matches[2]][] = $ext;
      $extensions[$ext] = "${matches[1]}/${matches[2]}";
    }
  }
}

fclose($stdin);

// Sort by mime group
ksort($mimes);

// Pad all mime-types to be this long
$pad = 40;

echo "types {";
foreach ($mimes as $group => $ids) {
  echo "\n";

  // Sort by extension
  asort($ids);

  foreach ($ids as $id => $types) {
    echo '  ' . str_pad("$group/$id", $pad) . implode(' ', $types) . ";\n";
  }
}
echo "}\n";

View Gist