ThisCalculator

Everyday

How to Enable Gzip Compression on Nginx & Fix Missing File Types

Updated · Standard formula, shown below

How to Enable Gzip Compression on Nginx & Fix Missing File Types

This guide shows you how to turn on gzip compression in Nginx to speed up your website by shrinking files before they are sent to browsers, and how to fix the common “missing file type” error that appears when MIME types are not set correctly. You will edit your Nginx configuration, test it, and reload the service—no prior Nginx experience is needed, but you should have basic command-line comfort.

What You’ll Need

  • Access to your server via SSH (or a terminal if you’re on the same machine).
  • Root or sudo privileges to edit Nginx configuration files.
  • Nginx already installed and running (the steps assume a standard Linux setup, e.g., Ubuntu/Debian or CentOS).
  • A text editor like nano or vim (we’ll use nano in examples).

Step-by-Step

  1. Locate your Nginx configuration files. The main config is usually at /etc/nginx/nginx.conf. Site-specific configs are in /etc/nginx/sites-available/ (and symlinked in sites-enabled/) on Debian/Ubuntu, or in /etc/nginx/conf.d/ on CentOS/RHEL. Open the main config first: sudo nano /etc/nginx/nginx.conf.
  2. Enable gzip in the http block. Inside the http { } block, add or uncomment these lines (they may already exist but be commented out):
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/rss+xml image/svg+xml;
    • gzip on; turns compression on.
    • gzip_comp_level 6 balances compression ratio and CPU usage (1-9, 6 is a good default).
    • gzip_min_length 256 skips very small files that wouldn’t benefit.
    • gzip_types lists the MIME types to compress. Add any types you need, like font/woff2 or application/wasm.
  3. Save and test the configuration. After editing, save (Ctrl+O, then Enter, then Ctrl+X in nano) and run: sudo nginx -t. This checks for syntax errors. You should see syntax is ok and test is successful.
  4. Reload Nginx. Apply the changes without dropping connections: sudo systemctl reload nginx (or sudo service nginx reload on older systems).
  5. Verify gzip is working. Use curl to check the response headers: curl -H "Accept-Encoding: gzip" -I https://yourdomain.com. Look for Content-Encoding: gzip in the response. If you see it, compression is active.
  6. Fix missing file types. The “missing file type” error usually appears as nginx: [warn] the "mime.types" file was not found or a 404 for files like .css or .js. This happens when the include mime.types; directive is missing from the http block. Open your main config and ensure this line is present inside http { }: include /etc/nginx/mime.types; (the path may vary, but it’s usually /etc/nginx/mime.types). If the file doesn’t exist, create it by copying the default: sudo cp /etc/nginx/mime.types.default /etc/nginx/mime.types (on some systems it’s mime.types already).
  7. Also check the server block. Sometimes the missing type error comes from a server block that overrides the http block. Make sure no include mime.types; is accidentally removed there. You can also add default_type application/octet-stream; as a fallback, but it’s better to rely on mime.types.
  8. Test and reload again. After fixing the mime types, run sudo nginx -t and then sudo systemctl reload nginx.

Common Mistakes

  • Forgetting to include mime.types – The most common cause of missing file types. Always have include mime.types; in the http block.
  • Not adding gzip_types – If you only enable gzip without specifying types, Nginx compresses only HTML by default. Add CSS, JS, JSON, etc.
  • Using gzip on already compressed files — Images (JPEG, PNG), video, and archives are already compressed; compressing them wastes CPU and may increase size. Don’t add them to gzip_types.
  • Setting gzip_comp_level too high – Level 9 uses more CPU for negligible gains. Stick to 5-6 unless you have spare CPU.
  • Editing the wrong config file – On some setups, the http block is in nginx.conf, but on others it might be in a file inside conf.d. Make sure you edit the one that’s actually loaded.
  • Not testing before reload – Always run nginx -t to avoid breaking your site with a syntax error.

Tips

  • Use gzip_vary on; to send the Vary: Accept-Encoding header, which helps caching proxies serve the correct version.
  • For high-traffic sites, consider enabling gzip_static on; to serve pre-compressed .gz files if you generate them, reducing CPU load.
  • Check your site’s performance with online tools like GTmetrix or Google PageSpeed Insights; they’ll show if gzip is active.
  • If you use a CDN, ensure it also respects gzip; most do automatically.
  • For very small files (under ~256 bytes), gzip overhead makes them larger, so keep gzip_min_length reasonable.

Troubleshooting

  • Gzip not working despite config – Check that the browser/client actually sends Accept-Encoding: gzip. Use curl -H "Accept-Encoding: gzip" -I to test. Also ensure the file type is in your gzip_types list. If you’re behind a proxy, it might strip the header; check with curl -H "Accept-Encoding: gzip" -I --proxy.
  • Missing file type error persists – Run nginx -t to see the exact error. If it says open() "/etc/nginx/mime.types" failed, the file is missing. Create it as described in Step 6. If it says unknown directive "include mime.types;", you have a typo or the path is wrong.
  • Nginx won’t reload – Check the error log: sudo tail -f /var/log/nginx/error.log. Most likely a syntax error in your config. Fix and test again.
  • Files still served without gzip – Some browsers (like old IE) don’t support gzip, but modern ones do. Also, if you have a cache plugin, it might serve cached uncompressed versions; clear the cache.
  • CPU usage spikes after enabling gzip – Lower gzip_comp_level to 2-3, or enable gzip_static to serve pre-compressed files.

Frequently Asked Questions

  1. What is gzip compression in Nginx? It’s a method to reduce the size of files sent from your server to browsers, making pages load faster. Nginx compresses files on the fly before sending them.
  2. Does gzip work with HTTPS? Yes, gzip works over HTTPS without any extra configuration. It’s done at the application layer, before encryption.
  3. Why do I get a “missing file type” error? This typically means Nginx can’t find the mime.types file, which maps file extensions to MIME types. Without it, Nginx sends a generic application/octet-stream type, causing browsers to download files instead of displaying them.
  4. Can I compress images with gzip? No, images are already compressed. Gzip is for text-based files like HTML, CSS, JavaScript, and JSON. Compressing images will waste CPU and may increase size.
  5. How do I know if gzip is enabled on my site? Use browser developer tools (Network tab) and look for Content-Encoding: gzip in the response headers. Or use curl as shown above.
  6. Will gzip break my existing site? No, gzip is transparent to users; browsers automatically decompress. It only affects performance, not functionality. However, always test after enabling.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.