{"id":700,"date":"2016-04-25T08:54:25","date_gmt":"2016-04-25T08:54:25","guid":{"rendered":"https:\/\/loadfocus.com\/blog\/?p=700"},"modified":"2022-08-04T11:47:47","modified_gmt":"2022-08-04T11:47:47","slug":"enable-gzip-compression-on-nginx","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/2016\/04\/enable-gzip-compression-on-nginx","title":{"rendered":"Enable gzip compression on NGINX"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>gzip on NGINX is a few lines of config and usually the cheapest bandwidth win available.<\/li>\n<li>Compress text types only. Compressing already-compressed images wastes CPU for nothing.<\/li>\n<li>Set a sensible minimum length, since compressing very small responses costs more than it saves.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr -->During Speed testing of your website or web application using LoadFocus (<a href=\"https:\/\/loadfocus.com\/website-speed-testing\">https:\/\/loadfocus.com\/website-speed-testing<\/a>) on the Advice section you might get the tip that you need to enable compression like in the image below:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1874\" height=\"822\" class=\"aligncenter size-full wp-image-706\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/advice_enable_compression.png\" alt=\"advice_enable_compression\"><\/p>\n<p>&nbsp;<\/p>\n<p>Compressing responses often significantly reduces the size of transmitted data.<br \/>\nBecause the compression happens at runtime this can add a big processing overhead which can affect the performance in a negative way.<\/p>\n<p>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.<\/p>\n<p>In the next tutorial we will show how you can enable compression in case you are using Nginx to serve your files.<\/p>\n<p>1. First locate the Nginx configuration file &#8211; nginx.conf<br \/>\n2. Once you located the file open the file and make sure that inside the configuration you can find the following flags:<\/p>\n<pre class=\"lang-java\"><code>\ngzip on;\ngzip_disable \"msie6\";\n\n\ngzip_vary on;\ngzip_proxied any;\ngzip_comp_level 5;\ngzip_buffers 16 8k;\ngzip_http_version 1.1;\ngzip_min_length 1000;\ngzip_types text\/plain text\/css application\/json application\/x-javascript text\/xml application\/xml application\/xml+rss text\/javascript;\n<\/code><\/pre>\n<p>3. Explanation of the flags:<br \/>\n[emaillocker]<br \/>\n<strong>gzip<\/strong> on &#8211; enables compression<\/p>\n<p><strong>gzip_disable<\/strong> &#8220;msie6&#8221; &#8211; disable compression for IE6 as IE6 doesn&#8217;t always read and cache gzipped content properly and you end up with mangled code<\/p>\n<p><strong>gzip_vary<\/strong> on &#8211; enables \/ disables inserting the \u201cVary: Accept-Encoding\u201d response header field if the directives gzip, gzip_static, or gunzip are active<\/p>\n<p><strong>gzip_proxied<\/strong> any &#8211; 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 \u201cVia\u201d request header field<\/p>\n<p><strong>gzip_comp_level<\/strong> 5 &#8211; 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<\/p>\n<p><strong>gzip_buffers<\/strong> 16 8k &#8211; 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<\/p>\n<p><strong>gzip_http_version<\/strong> 1.1 &#8211; sets the minimum HTTP version of a request required to compress a response<\/p>\n<p><strong>gzip_min_length<\/strong> 1000 &#8211; specifies the minimum length of the response to compress; the default is 20 bytes; don&#8217;t compress anything that&#8217;s already small and unlikely to shrink<\/p>\n<p><strong>gzip_types<\/strong> text\/plain text\/css application\/json application\/x-javascript text\/xml application\/xml application\/xml+rss text\/javascript<br \/>\n&#8211; 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<\/p>\n<p>4. The flags that are most important are the &#8220;gzip on&#8221; and &#8220;gzip_types&#8221;<\/p>\n<p>5. The directives\/flags that configure compression can be included in the http context or in a server or location configuration block<\/p>\n<p>6. Restart Nginx by doing sudo service nginx restart<\/p>\n<p>7. After you&#8217;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.<br \/>\n[\/emaillocker]<br \/>\n<a title=\"website load testing\" href=\"https:\/\/loadfocus.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">LoadFocus.com<\/a> is a Cloud Testing Platform used for Load and Performance Testing for Websites and APIs&nbsp;and&nbsp;Website Speed Testing with Analytics.<!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How much does gzip on NGINX help?<\/h3>\n<p>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.<\/p>\n<h3>What should I compress?<\/h3>\n<p>Text types: HTML, CSS, JavaScript, JSON, SVG. Images and video are already compressed, so running them through gzip again burns CPU for no gain.<\/p>\n<h3>Is there a downside?<\/h3>\n<p>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.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2016\/04\/5-tips-to-boost-page-speed-and-website-seo\">5 Tips to Boost Page Speed and Website SEO<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2024\/07\/from-slow-to-superfast-how-to-improve-your-website-speed-in-10-easy-steps\">Improve Your Website Speed in 10 Easy Steps<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2021\/12\/why-you-should-start-improving-the-images-on-your-website\">Why you should start improving the images on your website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>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&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/2016\/04\/enable-gzip-compression-on-nginx\" class=\"more-link\" title=\"Read Enable gzip compression on NGINX\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3677,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,104],"tags":[128,129,127],"class_list":["post-700","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-load-testing","category-website-speed-testing","tag-enable-compression","tag-nginx","tag-nginx-compression"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/comments?post=700"}],"version-history":[{"count":0,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/700\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/media\/3677"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/media?parent=700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/categories?post=700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/tags?post=700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}