0
0
Nginxdevops~10 mins

Default type handling in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Default type handling
Client Request
Check file extension
Match MIME type in types block?
NoUse default_type
Yes
Set Content-Type header
Send Response
Nginx checks the requested file's extension to find a matching MIME type. If none is found, it uses the default_type to set the Content-Type header.
Execution Sample
Nginx
location / {
    types {
        text/html html;
        image/png png;
    }
    default_type application/octet-stream;
}
This config sets MIME types for .html and .png files and uses application/octet-stream as the default Content-Type if no match is found.
Process Table
StepRequested FileExtensionMIME Type Found?Content-Type SetAction
1/index.htmlhtmlYestext/htmlServe with text/html
2/image.pngpngYesimage/pngServe with image/png
3/file.unknownunknownNoapplication/octet-streamServe with default_type
4/noextensionNoapplication/octet-streamServe with default_type
💡 Requests without matching extension use default_type for Content-Type header.
Status Tracker
VariableStartAfter 1After 2After 3After 4
Content-Typeunsettext/htmlimage/pngapplication/octet-streamapplication/octet-stream
Key Moments - 2 Insights
Why does nginx use application/octet-stream for some files?
When the file extension is not listed in the types block (see execution_table rows 3 and 4), nginx uses the default_type value to set the Content-Type header.
What happens if a requested file has no extension?
Nginx cannot find a matching MIME type, so it uses default_type as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what Content-Type is set for /image.png?
Atext/html
Bimage/png
Capplication/octet-stream
Dunknown
💡 Hint
Check execution_table row 2 under Content-Type Set column.
At which step does nginx use the default_type for Content-Type?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at execution_table rows where MIME Type Found? is No.
If you add 'application/json json;' to types, what Content-Type will /data.json get?
Aapplication/json
Bapplication/octet-stream
Ctext/html
Dimage/png
💡 Hint
Adding a type for .json means nginx finds a MIME type instead of using default_type.
Concept Snapshot
Nginx uses the 'types' block to map file extensions to MIME types.
If no match is found, it uses 'default_type' to set Content-Type.
This ensures every response has a Content-Type header.
Example: default_type application/octet-stream;
Files without known extensions get this default MIME type.
Full Transcript
Nginx handles file requests by checking the file extension against a list of known MIME types defined in the 'types' block. If it finds a match, it sets the Content-Type header accordingly. If no match is found, nginx uses the 'default_type' directive to set a fallback Content-Type header, usually 'application/octet-stream'. This process ensures that every response has a Content-Type header, which helps browsers understand how to handle the file. For example, a request for '/index.html' sets Content-Type to 'text/html', while a request for '/file.unknown' uses the default type. Files without extensions also use the default type. This behavior is important to configure correctly for proper content delivery.