enable gzip compression with nginx

2019-01-21 09:35发布

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?

标签: nginx gzip
9条回答
Rolldiameter
2楼-- · 2019-01-21 10:12

You need to run:

curl -I --compressed my_js_file

to make curl send an Accept-Encoding header for gzip - the server will only compress content if the client sends a header saying it will accept it.

NB you can write:

gzip_disable "msi6"

rather than using a regex to disable in IE 5.5 and 6, and you needn't specify text/html as a type because it is always compressed as long as gzip is activated.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-21 10:14

I had to enable gzip in my /etc/nginx/nginx.conf configuration:

gzip on;
gzip_disable "msie6";

gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Please note that I had to add application/javascript to the standard gzip_types configuration.

查看更多
混吃等死
4楼-- · 2019-01-21 10:14

Just like Alaa I had to add gzip_http_version 1.0; (no version was previously specified) for it to work (I tried on Firefox 27.0.0).

查看更多
登录 后发表回答