Key takeaways

  • gzip on NGINX is a few lines of config and usually the cheapest bandwidth win available.
  • Compress text types only. Compressing already-compressed images wastes CPU for nothing.
  • Set a sensible minimum length, since compressing very small responses costs more than it saves.

During Speed testing of your website or web application using LoadFocus (https://loadfocus.com/website-speed-testing) on the Advice section you might get the tip that you need to enable compression like in the image below:

Is Your Infrastructure Ready for Global Traffic Spikes?

Unexpected load surges can disrupt your services. With LoadFocus’s cutting-edge Load Testing solutions, simulate real-world traffic from multiple global locations in a single test. Our advanced engine dynamically upscales and downscales virtual users in real time, delivering comprehensive reports that empower you to identify and resolve performance bottlenecks before they affect your users.

View Pricing
Real-time insights
Discover More
Global scalability

 

advice_enable_compression

 

Think your website can handle a traffic spike?

Fair enough, but why leave it to chance? Uncover your website’s true limits with LoadFocus’s cloud-based Load Testing for Web Apps, Websites, and APIs. Avoid the risk of costly downtimes and missed opportunities—find out before your users do!

Effortless setup No coding required

Compressing responses often significantly reduces the size of transmitted data.
Because the compression happens at runtime this can add a big processing overhead which can affect the performance in a negative way.

Nginx if configured correctly compresses the responses before sending them to the clients and does not double compress the responses which have been already compressed.

In the next tutorial we will show how you can enable compression in case you are using Nginx to serve your files.

1. First locate the Nginx configuration file – nginx.conf
2. Once you located the file open the file and make sure that inside the configuration you can find the following flags:

LoadFocus is an all-in-one Cloud Testing Platform for Websites and APIs for Load Testing, Apache JMeter Load Testing, Page Speed Monitoring and API Monitoring!

Effortless setup No coding required

gzip on;
gzip_disable "msie6";


gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 1000;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

3. Explanation of the flags:
[emaillocker]
gzip on – enables compression

gzip_disable “msie6” – disable compression for IE6 as IE6 doesn’t always read and cache gzipped content properly and you end up with mangled code

gzip_vary on – enables / disables inserting the “Vary: Accept-Encoding” response header field if the directives gzip, gzip_static, or gunzip are active

gzip_proxied any – enables or disables gzipping of responses for proxied requests depending on the request and response; the fact that the request is proxied is determined by the presence of the “Via” request header field

gzip_comp_level 5 – sets a gzip compression level of a response; the values that are acceptable are between 1 and 9; 5 is a good compromise between size and cpu usage

gzip_buffers 16 8k – sets the number and size of buffers used to compress a response; by default, the buffer size is equal to either 4K or 8K, depending on a platform

gzip_http_version 1.1 – sets the minimum HTTP version of a request required to compress a response

gzip_min_length 1000 – specifies the minimum length of the response to compress; the default is 20 bytes; don’t compress anything that’s already small and unlikely to shrink

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript
– by default gzip compression is applied only to responses with MIME type text/html; to enable it for other types of MIME we need to add them to this flag

4. The flags that are most important are the “gzip on” and “gzip_types”

5. The directives/flags that configure compression can be included in the http context or in a server or location configuration block

6. Restart Nginx by doing sudo service nginx restart

7. After you’ve finishes to enable compression on your server run the LoadFocus speed test again. This time the tip regarding to Enable compression should say that everything is fine.
[/emaillocker]
LoadFocus.com is a Cloud Testing Platform used for Load and Performance Testing for Websites and APIs and Website Speed Testing with Analytics.

Frequently Asked Questions

How much does gzip on NGINX help?

It is usually the cheapest bandwidth saving available, often a large reduction on text responses for a few lines of configuration. Speed test advice flagging compression is normally pointing at this.

What should I compress?

Text types: HTML, CSS, JavaScript, JSON, SVG. Images and video are already compressed, so running them through gzip again burns CPU for no gain.

Is there a downside?

Compression happens at request time, so it costs processing. Set a minimum length as well, because compressing very small responses can cost more than the bytes it saves.

Related reading

Bogdan
Founder at LoadFocus

Bogdan builds and runs the tools this blog is about. He writes from what the products actually do in production, including the parts that break.

How fast is your website? Free Website Speed Test