Lighttpd
上QQ阅读APP看书,第一时间看更新

Mime Types

To give the client a hint of what to do with a file, the HTTP protocol defines that each file should be sent with a Mime type. A Mime type consists of a Content type and a subtype. The Content type is one of application, audio, example, image, message, model, multipart, text, and video. Subtypes can be registered with IANA by a Web form. The Internet Assigned Numbers Authority (IANA) maintains a list of mime types. Most Linux or BSD systems have a local list at /etc/mime.types. The authoritative list can be found at http://www.iana.org/assignments/media-types.

Note

All mime types

You can download a mime-types.py python script that uses the mime type module to create a mime type mapping suitable for inclusion in a Lighttpd configuration at http://packtpub.com/files/code/2103_Code.zip. Start the script with python mime-types.py and it writes a mime-types.conf file in the current directory.

If you do not have a python interpreter, get one from http://www.python.org.

For a single web project, a fairly small map of mime types will usually suffice:

mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".gif" => "image/gif",
".png" => "image/png",
".zip" => "application/zip",
".tar.gz" => "application/x-tgz",
".gz" => "application/x-gzip")

Note the last two entries; the order is of the essence here. Had we swapped them, any .tar.gz file would match .gz and would thus be served as application/x-gzip.

Note

Default mime type

Using an empty extension, you can define a default mime type that will be used if no matching extension can be found, like we did in our first lighttpd.conf example.

The standard mime type for an HTML page is "text/html", but this changes if you use XHTML, which can be sent as "text/html", "text/xml" or "application/xml", the correct type being "application/xhtml+xml". Some browsers react differently on each type. Storing them with the extension " .xhtml" will make it easier to distinguish between HTML and XHTML files.

If the file system in use supports XFS-style attributes, we can set the mime type for each file with the attr program:

attr -s Content-Type -V image/jpeg foto.jpg

This Content-Type attribute can be used wherever it is present by setting

mimetype.use-xattr = "enable"

in our lighttpd.conf file, Lighttpd will fall back to the mime-type.assignment for files without Content-Type attribute.