0
0
Nginxdevops~5 mins

Default type handling in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a web server sends files to a browser, it needs to tell the browser what type of file it is. This is called the MIME type. Nginx uses default type handling to decide the correct type for files it serves, so browsers know how to display or handle them.
When you want to serve static files like images, HTML, or CSS and ensure browsers understand them correctly.
When you add new file types to your website and want Nginx to recognize and serve them with the right content type.
When you want to avoid browsers treating files as plain text or downloads by mistake.
When you want to customize or extend the list of file types Nginx recognizes.
When you want to improve website security by correctly specifying file types.
Config File - nginx.conf
nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

The include mime.types; line loads a file that maps file extensions to MIME types.

The default_type application/octet-stream; line sets the default MIME type for files that don't match any known extension.

The server block defines a basic web server listening on port 80 serving files from /usr/share/nginx/html.

Commands
This command tests the Nginx configuration file for syntax errors before starting or reloading the server.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads the Nginx server to apply the new configuration without stopping the service.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command requests the headers of the index.html file to check the Content-Type header sent by Nginx.
Terminal
curl -I http://localhost/index.html
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Thu, 01 Jun 2024 12:00:00 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 31 May 2024 10:00:00 GMT Connection: keep-alive ETag: "609d-5a1c9f8e8f400" Accept-Ranges: bytes
Key Concept

If you remember nothing else from this pattern, remember: Nginx uses the mime.types file to map file extensions to content types and falls back to default_type when unknown.

Common Mistakes
Not including the mime.types file in the configuration.
Without it, Nginx cannot map file extensions to correct MIME types and will use the default for all files, which may cause browsers to misinterpret files.
Always include the mime.types file with the line 'include mime.types;' inside the http block.
Setting default_type to text/plain.
This causes unknown files to be served as plain text, which can expose source code or cause security issues.
Set default_type to application/octet-stream to safely treat unknown files as binary.
Forgetting to reload Nginx after changing the configuration.
Changes won't take effect until Nginx reloads or restarts, so the server keeps using old settings.
Run 'systemctl reload nginx' or 'nginx -s reload' after config changes.
Summary
Include the mime.types file in nginx.conf to map file extensions to MIME types.
Set default_type to application/octet-stream to handle unknown file types safely.
Test the configuration with 'nginx -t' and reload Nginx to apply changes.
Use curl or a browser to verify the Content-Type header sent by Nginx.