Why Does Nginx Warn About Duplicate MIME Type "Text/Html"?

Published July 25, 2024

Problem: Nginx Warning About Duplicate MIME Type

Nginx may show a warning about duplicate MIME type "text/html" during server configuration. This warning can confuse users and raise questions about the server setup. Understanding why this warning appears is useful for proper Nginx configuration.

Resolving the Nginx MIME Type Warning

Removing Redundant MIME Types

To fix the configuration and remove the warning about duplicate MIME types:

  1. Open your Nginx configuration file (/etc/nginx/nginx.conf).
  2. Find the line with the gzip_types directive.
  3. Remove "text/html" from the MIME types list.
  4. Save the file and restart Nginx.

The correct syntax for the gzip_types directive should look like this:

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

By removing "text/html" from the list, you avoid the duplicate MIME type warning. Nginx includes "text/html" by default for gzip compression, so you don't need to specify it again in the configuration.

After these changes, Nginx should start without the warning about duplicate MIME types. This adjustment helps keep a clean configuration for your web server.

Tip: Check for Other Duplicate MIME Types

When fixing the MIME type warning, it's a good idea to review your entire Nginx configuration for other potential duplicate MIME types. Look for any repeated entries in directives like 'types' or 'map' blocks. Removing these duplicates can help optimize your configuration and prevent similar warnings in the future.

Optimizing Nginx MIME Type Configuration

Best Practices for MIME Type Settings

When setting up MIME types in Nginx, follow these tips for a good setup:

  1. Use needed MIME types: Add only the MIME types your web application uses. This keeps your setup simple and easy to manage.

  2. Group similar MIME types: Arrange your MIME types by grouping them based on their use or file type. For example, put all image formats together.

  3. Use wildcard patterns: For file extensions with multiple MIME types, use wildcard patterns to cover all options. For example, use "image/*" to include all image formats.

  4. Update the setup: Check and update your MIME type settings often to support new file formats your web application might use.

  5. Set a default MIME type: Use a default MIME type for unknown file types to avoid security issues.

Tip: Use gzip compression

Enable gzip compression for text-based MIME types to reduce file sizes and improve load times. Add the following to your Nginx configuration:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Common MIME types for web applications:

  • text/css for CSS files
  • application/javascript for JavaScript files
  • image/jpeg, image/png, image/gif for common image formats
  • application/json for JSON data
  • text/xml and application/xml for XML files
  • application/pdf for PDF documents
  • audio/mpeg, audio/ogg for audio files
  • video/mp4, video/webm for video files
  • font/woff, font/woff2 for web fonts