Bird
Raised Fist0
Nginxdevops~20 mins

Default type handling in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
NGINX MIME Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does NGINX determine the MIME type of a served file by default?

When NGINX serves a static file, how does it decide which MIME type to send in the Content-Type header if no explicit type is set?

AIt uses the file extension and the <code>mime.types</code> file to map extensions to MIME types.
BIt always sends <code>application/octet-stream</code> regardless of file type.
CIt guesses the MIME type by reading the file content.
DIt uses the <code>default_type</code> directive only and ignores file extensions.
Attempts:
2 left
💡 Hint

Think about how web servers usually identify file types for HTTP headers.

💻 Command Output
intermediate
2:00remaining
Output of NGINX with missing MIME type for .xyz files

Given this NGINX configuration snippet:

http {
    include       mime.types;
    default_type  application/octet-stream;
}
server {
    listen 8080;
    location / {
        root /usr/share/nginx/html;
    }
}

If a client requests file.xyz (an unknown extension), what Content-Type header will NGINX send?

ANo Content-Type header is sent
BContent-Type: application/octet-stream
CContent-Type: application/xyz
DContent-Type: text/plain
Attempts:
2 left
💡 Hint

Consider what default_type is set to and what happens if the extension is unknown.

Configuration
advanced
2:00remaining
Correctly setting default MIME type for unknown files

You want NGINX to send text/plain as the default MIME type for files with unknown extensions. Which configuration snippet achieves this?

A
http {
    include mime.types;
    default_type text/plain;
}
B
http {
    default_type text/plain;
    include mime.types;
}
C
http {
    include mime.types;
    default_type application/octet-stream;
    default_type text/plain;
}
D
http {
    include mime.types;
    default_type;
}
Attempts:
2 left
💡 Hint

The order of directives matters and default_type must have a valid value.

Troubleshoot
advanced
2:00remaining
Why is NGINX sending 'Content-Type: text/html' for all files?

An NGINX server is sending Content-Type: text/html for every file, including images and CSS. The config includes include mime.types; and default_type application/octet-stream;. What is the most likely cause?

AThe <code>root</code> directive is missing, causing NGINX to default to <code>text/html</code>.
BThe <code>default_type</code> directive is ignored if <code>include mime.types;</code> is present.
CNGINX caches MIME types and needs a restart to apply changes.
DThe <code>mime.types</code> file is missing or empty, so NGINX falls back to <code>default_type</code> which is overridden elsewhere.
Attempts:
2 left
💡 Hint

Think about what happens if the MIME types file is not loaded properly.

🔀 Workflow
expert
2:00remaining
Order of directives affecting MIME type handling

Consider this NGINX http block:

http {
    default_type application/json;
    include mime.types;
}

What MIME type will NGINX send for a .css file request?

Aapplication/json
Bapplication/octet-stream
Ctext/css
DNo Content-Type header
Attempts:
2 left
💡 Hint

NGINX prioritizes MIME type lookup from the types map (mime.types) before using default_type.

Practice

(1/5)
1. What does the default_type directive do in an nginx configuration?
easy
A. Sets the MIME type for files without a known extension
B. Defines the default server port
C. Specifies the default root directory
D. Enables gzip compression by default

Solution

  1. Step 1: Understand the purpose of default_type

    The default_type directive tells nginx what MIME type to use when it cannot determine the file type from the extension.
  2. Step 2: Match the directive to its function

    Since it sets the fallback MIME type, it helps browsers know how to handle unknown files.
  3. Final Answer:

    Sets the MIME type for files without a known extension -> Option A
  4. Quick Check:

    default_type sets fallback MIME type = A [OK]
Hint: default_type sets fallback MIME type for unknown files [OK]
Common Mistakes:
  • Confusing default_type with server port settings
  • Thinking it sets root directory
  • Assuming it enables compression
2. Which of the following is the correct syntax to set the default MIME type to application/json inside an nginx http block?
easy
A. default_type 'application/json';
B. default_type application/json;
C. default_type: application/json;
D. default_type = application/json;

Solution

  1. Step 1: Recall nginx directive syntax

    nginx directives use the format: directive_name value; without equals or colons.
  2. Step 2: Identify the correct syntax for default_type

    The correct syntax is default_type application/json; with no quotes needed.
  3. Final Answer:

    default_type application/json; -> Option B
  4. Quick Check:

    Correct nginx directive syntax = D [OK]
Hint: nginx directives end with semicolon, no equals or colon [OK]
Common Mistakes:
  • Using equals sign (=) in directive
  • Adding colon (:) after directive
  • Putting quotes around MIME type unnecessarily
3. Given this nginx config snippet inside a location block:
default_type text/plain;

What will be the Content-Type header for a request to a file named unknownfile.xyz?
medium
A. text/plain
B. application/octet-stream
C. application/json
D. text/html

Solution

  1. Step 1: Identify the default_type setting

    The config sets default_type text/plain; inside the location block.
  2. Step 2: Determine MIME type for unknown extension

    Since .xyz is unknown, nginx uses the default_type value text/plain as Content-Type.
  3. Final Answer:

    text/plain -> Option A
  4. Quick Check:

    Unknown file uses default_type = A [OK]
Hint: Unknown extensions get default_type MIME type [OK]
Common Mistakes:
  • Assuming default is application/octet-stream
  • Confusing with text/html default
  • Ignoring location block override
4. You set default_type application/json; in your nginx server block, but requests to unknown file types still return text/html. What is the most likely cause?
medium
A. You forgot to reload nginx after changing config
B. The file extension is recognized, so default_type is ignored
C. default_type cannot be set in server block
D. Another default_type directive in a location block overrides it

Solution

  1. Step 1: Understand nginx directive inheritance

    Directives in location blocks override those in server blocks.
  2. Step 2: Identify override causing unexpected Content-Type

    If a location block sets default_type text/html;, it will override the server block setting.
  3. Final Answer:

    Another default_type directive in a location block overrides it -> Option D
  4. Quick Check:

    Location block overrides server block default_type = B [OK]
Hint: Location block default_type overrides server block [OK]
Common Mistakes:
  • Thinking default_type can't be set in server block
  • Forgetting to reload nginx
  • Assuming file extension is always unknown
5. You want nginx to serve unknown file types as application/octet-stream globally, but for a specific /images location, serve unknown files as image/png. Which configuration correctly achieves this?
hard
A. Set default_type application/octet-stream; only in location /images
B. In server block: default_type image/png; and in location /images: default_type application/octet-stream;
C. In http block: default_type application/octet-stream; and in location /images: default_type image/png;
D. Set default_type image/png; only in http block

Solution

  1. Step 1: Set global default_type in http block

    Setting default_type application/octet-stream; in http block applies globally to all requests.
  2. Step 2: Override default_type in specific location

    Setting default_type image/png; inside location /images overrides the global setting for that path.
  3. Final Answer:

    In http block: default_type application/octet-stream; and in location /images: default_type image/png; -> Option C
  4. Quick Check:

    Global default_type overridden by location block = C [OK]
Hint: Set global default_type in http, override in location [OK]
Common Mistakes:
  • Reversing global and location settings
  • Setting default_type only in location without global
  • Using server block instead of http for global