gzip(GNU- ZIP)是一種壓縮技術。經過gzip壓縮后頁面大小可以變為原來的30%甚至更小,這樣,用戶瀏覽頁面的時候速度會塊得多。gzip的壓縮頁面需要瀏覽 器和服務器雙方都支持,實際上就是服務器端壓縮,傳到瀏覽器后瀏覽器解壓并解析。瀏覽器那里不需要我們擔心,因為目前的巨大多數瀏覽器都支持解析gzip 過的頁面。
Nginx的壓縮輸出有一組gzip壓縮指令來實現。相關指令位于http{….}兩個大括號之間。
下面大致講一下配置開啟gzip壓縮的方法:
1、Vim打開Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
2、找到如下一段,進行修改
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]/.";
3、解釋一下
第1行:開啟Gzip
第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了
這里再講一下gzip_proxied的相關配置參數:
syntax: gzip_proxied off |expired | no-cache |no-store | private |no_last_modified | no_etag |auth | any ...;default: gzip_proxied off;
Nginx作為反向代理的時候啟用,開啟或者關閉后端服務器返回的結果,匹配的前提是后端服務器必須要返回包含"Via"的 header頭。
off
關閉所有的代理結果數據的壓縮
expired
啟用壓縮,如果header頭中包含 "Expires" 頭信息
no-cache
啟用壓縮,如果header頭中包含 "Cache-Control:no-cache" 頭信息
no-store
啟用壓縮,如果header頭中包含 "Cache-Control:no-store" 頭信息
private
啟用壓縮,如果header頭中包含 "Cache-Control:private" 頭信息
no_last_modified
啟用壓縮,如果header頭中不包含 "Last-Modified" 頭信息
no_etag
啟用壓縮 ,如果header頭中不包含 "ETag" 頭信息
auth
用壓縮 , 如果header頭中包含 "Authorization" 頭信息
any
無條件啟用壓縮
4、:wq保存退出,重新加載Nginx
/usr/local/nginx/sbin/nginx -s reload
5、用curl測試Gzip是否成功開啟
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/"
HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:13:09 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.2.17p1X-Pingback: //www.slyar.com/blog/xmlrpc.phpContent-Encoding: gzip
頁面成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "//www.49028c.com/blog/wp-content/plugins/photonic/include/css/photonic.css"
HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:25 GMTContent-Type: text/cssLast-Modified: Sun, 26 Aug 2012 15:17:07 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:25 GMTCache-Control: max-age=43200Content-Encoding: gzip
css文件成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "//www.slyar.com/blog/wp-includes/js/jquery/jquery.js"
HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:38 GMTContent-Type: application/x-javascriptLast-Modified: Thu, 12 Jul 2012 17:42:45 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:38 GMTCache-Control: max-age=43200Content-Encoding: gzip
js文件成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "//www.49028c.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"
HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:22:45 GMTContent-Type: image/pngLast-Modified: Thu, 23 Aug 2012 13:50:53 GMTConnection: keep-aliveExpires: Tue, 25 Sep 2012 18:22:45 GMTCache-Control: max-age=2592000Content-Encoding: gzip
圖片成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "//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