0
0
Nginxdevops~10 mins

MIME types configuration in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - MIME types configuration
Client requests file
Nginx receives request
Check file extension
Look up MIME type in mime.types
Set Content-Type header
Send response with correct MIME type
Nginx checks the requested file's extension, finds the matching MIME type from its configuration, sets the Content-Type header, and sends the response.
Execution Sample
Nginx
location / {
    root /var/www/html;
    include mime.types;
    default_type application/octet-stream;
}
This config snippet tells nginx to serve files from /var/www/html, use mime.types to find MIME types, and use a default if none matches.
Process Table
StepActionFile RequestedFile ExtensionMIME Type FoundContent-Type Header Set
1Receive request/index.html.htmltext/htmltext/html
2Receive request/image.png.pngimage/pngimage/png
3Receive request/script.js.jsapplication/javascriptapplication/javascript
4Receive request/unknownfile.xyz.xyznoneapplication/octet-stream
5Receive request/style.css.csstext/csstext/css
💡 All requests processed by matching extension to MIME type or using default if none found.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5
Requested File/index.html/image.png/script.js/unknownfile.xyz/style.css
File Extension.html.png.js.xyz.css
MIME Typetext/htmlimage/pngapplication/javascriptnonetext/css
Content-Type Headertext/htmlimage/pngapplication/javascriptapplication/octet-streamtext/css
Key Moments - 2 Insights
Why does nginx use 'application/octet-stream' for unknown file extensions?
When the file extension is not found in mime.types (see step 4 in execution_table), nginx uses the default_type as a fallback to ensure the file is served with a safe generic MIME type.
How does nginx determine the MIME type for a requested file?
Nginx extracts the file extension from the requested file (execution_table column 'File Extension'), then looks it up in the mime.types file to find the matching MIME type.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what Content-Type header is set for '/script.js'?
Atext/javascript
Bapplication/javascript
Capplication/json
Dtext/plain
💡 Hint
Check the row where File Requested is '/script.js' and see the Content-Type Header column.
At which step does nginx use the default MIME type 'application/octet-stream'?
AStep 4
BStep 2
CStep 1
DStep 5
💡 Hint
Look for the step where MIME Type Found is 'none' and Content-Type Header is 'application/octet-stream'.
If a new file type '.md' with MIME 'text/markdown' is added to mime.types, what changes in the execution_table?
AAll '.html' files would be served as 'text/markdown'
BThe default_type would change to 'text/markdown'
CA new row with '.md' extension and 'text/markdown' MIME type would appear
DNo change unless nginx is restarted
💡 Hint
Adding a MIME type allows nginx to match new extensions and set correct Content-Type headers as shown in the execution_table rows.
Concept Snapshot
MIME types config in nginx:
- Use 'include mime.types;' to load types
- Match file extension to MIME type
- Set 'default_type' for unknown extensions
- Content-Type header tells browser file type
- Ensures correct file handling by clients
Full Transcript
When a client requests a file, nginx checks the file extension. It looks up this extension in the mime.types file to find the matching MIME type. If found, nginx sets the Content-Type header to that MIME type. If not found, it uses the default_type, usually 'application/octet-stream'. This header helps browsers know how to handle the file. The example shows requests for various files and how nginx sets the Content-Type accordingly.