Nginx開啓Gzip壓縮大幅提升頁面加載速度javascript
http://www.veryhuo.com/a/view/51706.htmlphp
剛剛給博客加了一個500px相冊插件,lightbox引入了不少js文件和css文件,頁面一會兒看起來很是臃腫,因此仍是把Gzip打開了。css
環境:Debian 6html
一、Vim打開Nginx配置文件java
vim /usr/local/nginx/conf/nginx.confjquery
二、找到以下一段,進行修改nginx
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";vim
三、解釋一下緩存
第1行:開啓Gzipapp
第2行:不壓縮臨界值,大於1K的才壓縮,通常不用改
第3行:buffer,就是,嗯,算了不解釋了,不用改
第4行:用了反向代理的話,末端通訊是HTTP/1.0,有需求的應該也不用看我這科普文了;有這句的話註釋了就好了,默認是HTTP/1.1
第5行:壓縮級別,1-10,數字越大壓縮的越好,時間也越長,看心情隨便改吧
第6行:進行壓縮的文件類型,缺啥補啥就好了,JavaScript有兩種寫法,最好都寫上吧,總有人抱怨js文件沒有壓縮,其實多寫一種格式就好了
第7行:跟Squid等緩存服務有關,on的話會在Header裏增長"Vary: Accept-Encoding",我不須要這玩意,本身對照狀況看着辦吧
第8行:IE6對Gzip不怎麼友好,不給它Gzip了
四、:wq保存退出,從新加載Nginx
/usr/local/nginx/sbin/nginx -s reload
五、用curl測試Gzip是否成功開啓
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip
頁面成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
css文件成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-includes/js/jquery/jquery.js"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
js文件成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
圖片成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
最後來個不到1K的文件,因爲個人閾值是1K,因此沒壓縮