Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enable gzip compression with nginx

Tags:

nginx

gzip

I am trying to enable gzip compression for components of my website. I have ubuntu 11.04 server and nginx 1.2.
in my nginx configuration of the website, i have this

gzip             on;
#gzip_min_length  1000;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
#it was gzip_buffers 16 8k; 
gzip_buffers 128 4k; #my pagesize is 4
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

and Yslow and google speed measures are advising me to use gzip to reduce transmission over network. now when i try to curl -I my_js_file i got

curl -I http://www.albawaba.com/sites/default/files/js/js_367664096ca6baf65052749f685cac7b.js
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 14 Apr 2013 13:15:43 GMT
Content-Type: application/x-javascript
Content-Length: 208463
Connection: keep-alive
Last-Modified: Sun, 14 Apr 2013 10:58:06 GMT
Vary: Accept-Encoding
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Pragma: public
Cache-Control: public
Accept-Ranges: bytes

any idea of what i have done wrong or what shall i do to get compressed content?

like image 381
Alaa Avatar asked Apr 14 '13 13:04

Alaa


People also ask

Does NGINX support gzip?

You can configure Nginx to use gzip to compress the files it serves on the fly. Those files are then decompressed by the browsers that support it upon retrieval with no loss whatsoever, but with the benefit of a smaller amount of data to transfer between the web server and browser.

How do I know if gzip is enabled NGINX?

You can tell using Developer Tools (F12). Go to the Network tab, select the file you want to examine and then look at the Headers tab on the right. If you are gzipped, then you will see that in the Content-Encoding.

Does NGINX compress by default?

By default, NGINX does not compress responses to proxied requests (requests that come from the proxy server).


3 Answers

As others have written, it's not enough to enable gzip compression in your server -- the client also needs to ask for it in its requests via the Accept-Encoding: gzip header (or a superset thereof). Modern browsers include this header automatically, but for curl you'll need to include one of the following in your command:

  • -H "Accept-Encoding: gzip" : You should see the Content-Encoding: gzip header in the response (might need to output headers with curl's -v flag), as well as some seemingly garbled output for the content, the actual gzip stream.
  • --compressed : You should still see Content-Encoding: gzip in the response headers, but curl knows to decompress the content before outputting it.
like image 76
lot Avatar answered Oct 04 '22 07:10

lot


I can't find anything obviously wrong with your config, usually gzip on & gzip_types application/x-javascript would be enough to get you going. If everything is working right you'll get a "Content-Encoding:gzip" returned back to you.

PLEASE KEEP IN MIND: I have much more consistency with GOOGLE DEVELOPER TOOLS (curl just doesn't behave the way a browser would).

In Chrome, right click and go to "inspect element" then go to "network" (then reload the page if you have to), then click on a resource and check the header tab, the output should look like this (notice the content-encoding is gzip, yay):

Request URL:https://ssl.gstatic.com/gb/js/sem_a3becc1f55aef317b63a03a400446790.js Request Method:GET Status Code:200 OK (from cache) Response Headersview source age:199067 cache-control:public, max-age=691200 content-encoding:gzip content-length:19132 content-type:text/javascript date:Fri, 12 Apr 2013 06:32:58 GMT expires:Sat, 20 Apr 2013 06:32:58 GMT last-modified:Sat, 23 Mar 2013 01:48:21 GMT server:sffe status:200 OK vary:Accept-Encoding version:HTTP/1.1 x-content-type-options:nosniff x-xss-protection:1; mode=block 

Anyway if you are SURE your content is not getting gzipped, I normally get up and running pretty fast with the following:

## Compression gzip              on; gzip_buffers      16 8k; gzip_comp_level   4; gzip_http_version 1.0; gzip_min_length   1280; gzip_types        text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp; gzip_vary         on; 

You could try this in replacement for your code, and/or tweak your values one at a time to help you localize your issue.

Remember to restart or reload nginx after changing the config.

It may also be useful to check your logs and see if there's anything interesting there should you still be stuck.

like image 26
Drew Khoury Avatar answered Oct 04 '22 06:10

Drew Khoury


I just changed gzip_http_version 1.1; to be gzip_http_version 1.0; and then it worked

like image 43
Alaa Avatar answered Oct 04 '22 08:10

Alaa