default_type
The `default_type` directive sets the default MIME type for files and responses when a specific type is not defined.
Description
The default_type directive in NGINX specifies the default media type that should be used for responses when the server is unable to determine the content type from the file extension or other factors. This directive is particularly useful in scenarios where files are served that may not have standard file extensions, or when the configuration does not include specific mappings for certain file types. By specifying a default type, you ensure that clients receive an appropriate Content-Type header, which can help with browser interpretation and handling of the data.
The directive can be defined in several contexts: http, server, and location, allowing for flexibility in its application across different scopes of configuration. The argument for default_type can be a standard MIME type such as 'text/html', 'application/json', or any other valid media type. Its placement in the configuration can determine whether all requests or only those in a specific server or location block receive the specified default type.
If no default type is declared, and NGINX cannot determine the file's type, it will not set a Content-Type header, which may lead to issues where clients do not handle the response correctly. Therefore, it is often a best practice to explicitly define a default type to maintain consistent behavior across served content.
Config Example
http {
default_type text/html;
server {
location / {
root /usr/share/nginx/html;
}
}
}Be careful with overriding default types in nested contexts; more specific declarations will take precedence.
Not setting a default type can lead to clients receiving incorrect Content-Type headers, affecting content display in browsers.