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
- 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 insites-enabled/) on Debian/Ubuntu, or in/etc/nginx/conf.d/on CentOS/RHEL. Open the main config first:sudo nano /etc/nginx/nginx.conf. - 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 6balances compression ratio and CPU usage (1-9, 6 is a good default).gzip_min_length 256skips very small files that wouldn’t benefit.gzip_typeslists the MIME types to compress. Add any types you need, likefont/woff2orapplication/wasm.
- 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 seesyntax is okandtest is successful. - Reload Nginx. Apply the changes without dropping connections:
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). - Verify gzip is working. Use curl to check the response headers:
curl -H "Accept-Encoding: gzip" -I https://yourdomain.com. Look forContent-Encoding: gzipin the response. If you see it, compression is active. - Fix missing file types. The “missing file type” error usually appears as
nginx: [warn] the "mime.types" file was not foundor a 404 for files like.cssor.js. This happens when theinclude mime.types;directive is missing from thehttpblock. Open your main config and ensure this line is present insidehttp { }: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’smime.typesalready). - 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 adddefault_type application/octet-stream;as a fallback, but it’s better to rely on mime.types. - Test and reload again. After fixing the mime types, run
sudo nginx -tand thensudo 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 insideconf.d. Make sure you edit the one that’s actually loaded. - Not testing before reload – Always run
nginx -tto avoid breaking your site with a syntax error.
Tips
- Use
gzip_vary on;to send theVary: Accept-Encodingheader, which helps caching proxies serve the correct version. - For high-traffic sites, consider enabling
gzip_static on;to serve pre-compressed.gzfiles 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_lengthreasonable.
Troubleshooting
- Gzip not working despite config – Check that the browser/client actually sends
Accept-Encoding: gzip. Usecurl -H "Accept-Encoding: gzip" -Ito test. Also ensure the file type is in yourgzip_typeslist. If you’re behind a proxy, it might strip the header; check withcurl -H "Accept-Encoding: gzip" -I --proxy. - Missing file type error persists – Run
nginx -tto see the exact error. If it saysopen() "/etc/nginx/mime.types" failed, the file is missing. Create it as described in Step 6. If it saysunknown 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_levelto 2-3, or enablegzip_staticto serve pre-compressed files.
Frequently Asked Questions
- 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.
- Does gzip work with HTTPS? Yes, gzip works over HTTPS without any extra configuration. It’s done at the application layer, before encryption.
- Why do I get a “missing file type” error? This typically means Nginx can’t find the
mime.typesfile, which maps file extensions to MIME types. Without it, Nginx sends a genericapplication/octet-streamtype, causing browsers to download files instead of displaying them. - 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.
- How do I know if gzip is enabled on my site? Use browser developer tools (Network tab) and look for
Content-Encoding: gzipin the response headers. Or use curl as shown above. - 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.