How to verify that NGINX Gzip Compression is working

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Gzip compression is a powerful method of compressing web files when they are being transferred from server to client, effectively decreasing the size of the transferred files and improving the web page load times. NGINX, a high-performance web server, has built-in capabilities for Gzip compression, which can be configured to enhance the performance of your web content delivery. In this tutorial, we’ll go through methods of verifying that Gzip compression is properly enabled and functioning on your NGINX server.

Enabling Gzip Compression

Before verifying that Gzip compression is working, you must first ensure that it is enabled on your NGINX server. Here’s how to do that:

http {
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_comp_level 6;

    # Other configurations...
}

This snippet should be added to the http block of your NGINX configuration file, which is usually located at /etc/nginx/nginx.conf. After adding the above code, you should test your NGINX configuration for syntax errors:

nginx -t

And then reload NGINX configuration to apply the changes:

sudo systemctl reload nginx

Using Curl to Check Gzip Compression

The curl command is a versatile tool for making HTTP requests from the command line, and it can also be used to check if Gzip compression is active. Execute the following:

curl -I -H 'Accept-Encoding: gzip, deflate' http://example.com

If Gzip compression is enabled and working correctly, you’ll observe Content-Encoding: gzip in the response headers indicating that the file has been compressed.

Online Gzip Test Tools

If you’d prefer a graphical user interface (GUI) to verify Gzip compression, there are various online tools available. Simply navigate to the website of the service, enter your site’s URL, and submit the form. The tool will display whether Gzip compression is enabled.

Popular online Gzip verification tools include:

Examining Browser DevTools

Web browsers such as Chrome and Firefox come with developer tools that can be used to delve into the HTTP requests and responses, including headers. To check if Gzip compression is enabled using browser tools, do the following:

  1. Open your browser and navigate to your website.
  2. Right-click on the page and select ‘Inspect’ or press Ctrl+Shift+I (Cmd+Option+I on Mac) to open the Developer Tools.
  3. Click on the ‘Network’ tab, then reload the page to collect the network activity information.
  4. Click on the file in the list of resources, and look at the response headers for Content-Encoding: gzip.

Seeing the gzip value in the response headers means that Gzip compression is indeed being applied.

Advanced: Analyzing Compression with Wireshark

For a more in-depth analysis, you can use Wireshark, a network protocol analyzer, which shows the actual packets being sent over the network:

  1. Open Wireshark and start capturing packets on the relevant network interface.
  2. Access your site from a browser to generate traffic.
  3. Stop the packet capture and use the Wireshark filter to find HTTP traffic to your site.
  4. Analyze the packet details to look for HTTP responses with the Content-Encoding: gzip header.

Writing Automated Tests

If you are a developer looking for an automated approach to verify Gzip compression across deployments, writing integration tests using tools like Selenium, Postman, Jest, etc., which simulate browser requests, is a good strategy. A simple check can be scripted to ensure that the specific response headers are being returned.

Conclusion

Verifying Gzip compression is a straightforward process, whether using command-line tools like curl, online test tools, browser DevTools, network protocol analyzers like Wireshark, or automation tests. By ensuring Gzip compression is operating effectively, you can provide an optimized user experience with faster page loads and reduced bandwidth usage.