Column 1
Skip to content
Column 1

How To Enable gzip With NGINX and Ubuntu 22.04

Gzip is a file compression method that reduces the size of HTML, CSS, and JavaScript files by up to 80%. Enabling gzip on your web server can significantly improve your website's loading speed and reduce bandwidth usage. This post will guide you on how to enable gzip compression with NGINX and Ubuntu 22.04.

Step 1: Check if gzip is enabled

Before proceeding with the steps, it's important to check if gzip is already enabled on your server. You can use the following command to check:

nginx -V 2>&1 | grep -o with-http_gzip_static_module

If you see with-http_gzip_static_module, it means that gzip is already enabled.

Step 2: Install gzip

If gzip is not already enabled, the first step is to install the gzip module:

sudo apt-get update
sudo apt-get install nginx-extras

Step 3: Configure gzip in NGINX

Next, you need to configure gzip in NGINX. Open the /etc/nginx/nginx.conf file in your favorite text editor:

sudo nano /etc/nginx/nginx.conf

Add the following lines to the file:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;

Save and close the file.

Step 4: Test gzip

To test if gzip is working, you can use an online tool like GTmetrix or Pingdom. Simply run a speed test for your website, and the tool will tell you if gzip is enabled or not.

In conclusion, enabling gzip compression with NGINX and Ubuntu 22.04 is a simple and effective way to improve your website's loading speed and reduce bandwidth usage. By following the steps outlined in this post, you can enable gzip and start enjoying the benefits it offers.

Hosting maintenance note

Compression changes should be validated before reload and measured afterward. Check that text assets are compressed, binary assets are not double-compressed, and CDN cache does not hide the result from your test.

Before changing a live site or server, capture the current configuration, confirm recent backups, and decide how you will roll back if the change affects logins, checkout, forms, cron jobs, or customer traffic. For shared hosting, make changes during a window where you can watch logs and customer-facing pages.

Post-change verification

  • Validate the server or application configuration before reloading services.
  • Test the exact workflow the change was meant to improve.
  • Clear only the cache layers needed for the test, then retest from the public edge.
  • Review logs for warnings, permission errors, failed requests, or unexpected redirects.

Related hosting performance reading

Common gzip verification mistakes

If a gzip test appears to fail, check the test path before changing Nginx again. CDN cache, browser cache, asset type, upstream proxy behavior, and already-compressed files can all make the result look inconsistent. Test a plain CSS or JavaScript asset, confirm the response headers, and compare origin and public edge responses. After a reload, review the Nginx error log before assuming the setting is production-ready.