Nginx Index Files and MIME Types Explained
Serve a directory URL with an index
A visitor often opens / rather than /index.html. The index directive provides an ordered list of candidate files for requests ending in /. For a normal static site, Nginx looks for a suitable file inside that directory; a chosen index triggers internal request processing for the index URI. A different location can handle that URI.
root html;index welcome.html index.html;In the supplied alternate configuration, / selects html/welcome.html because it exists and is listed first. If that candidate is absent and the second is available, index.html can be selected. A direct request for /index.html still names that file. This is not a browser redirect: the internal URI change normally leaves the visible address unchanged.
Tell the browser what the file contains
A media type, sometimes called a MIME type, describes the format of a response. The HTTP Content-Type header communicates it to the client. A CSS response should be labeled as CSS, not HTML; successful delivery of bytes is not sufficient for correct rendering.
types { text/html html; text/css css; image/svg+xml svg;}default_type application/octet-stream;This small types block maps file extensions to the intended media types. The lab chooses a binary fallback for unknown extensions such as .dat. Without an explicit default_type, the core directive defaults to text/plain. A fallback changes response metadata, not the file bytes. In larger sites, the distribution may provide a mime.types file with many more mappings.
include mime.types;default_type application/octet-stream;Use this alternative only when the included file exists relative to the configuration directory (or use its verified absolute path) and contains a suitable types block. Do not add a competing map at the same context by habit. A new types table in a child context can replace the inherited table, so confirm that all required asset types remain mapped.
Check actual response headers
All commands in this chapter are optional, for the supplied Chapter 5 lab only. Use a reviewed patched Nginx build in an approved disposable Linux VM as a non-root user. Follow the lab README to copy the files, set LAB, prepare directories, confirm port availability and test the complete configuration. Results are predicted, not observed.
curl --noproxy '*' --max-time 2 -I http://127.0.0.1:18085/assets/site.csscurl --noproxy '*' --max-time 2 -I http://127.0.0.1:18085/download.datThe -I option sends HEAD, a request for response metadata without a response body. Expect a 200 status with media type text/css for the stylesheet and application/octet-stream for the data file. Header parameters may vary. Use GET as well when validating the body.
/assets/site.css -> 200, Content-Type: text/css/assets/leaf.svg -> 200, Content-Type: image/svg+xml/download.dat -> 200, Content-Type: application/octet-streamPractice: predict the index and type
Given both welcome.html and index.html, predict the response to / under the alternate config and to /index.html directly. Then predict the type for /assets/leaf.svg. Next: distinguish missing-file, directory-index and permission errors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Index and media types
Check the concepts taught in this lesson.
Both welcome.html and index.html exist. With index welcome.html index.html, what does / select?
What does Content-Type communicate?
Which media type should the lab return for /assets/site.css?
What is the effect of default_type for an unmapped .dat file in the lab?
What does curl -I request?