爲了下降tomcat服務的壓力,把頁面上的圖片採用windows版的nginx進行加載,因爲有些圖片比較大,加載特別的慢,因此在nginx中打開了gzip的壓縮功能。加載圖片的速度快了不少。javascript
經過站長工具中的」網頁GZIP壓縮檢測」工具檢測圖片的壓縮率達到了69.53%,以下圖:
php
下面介紹nginx.conf文件是怎麼配置的:
css
一、打開nginx.conf配置文件;html
二、找到#gzip on這句,以下圖:
java
3.在把#gzip on 改爲下面代碼:
jquery
#開啓Gzip gzip on; #不壓縮臨界值,大於1K的才壓縮,通常不用改 gzip_min_length 100k; #設置gzip申請內存的大小,其做用是按塊大小的倍數申請內存空間 gzip_buffers 4 48k; #用了反向代理的話,末端通訊是HTTP/1.0,有需求的應該也不用看我這科普文了;有這句的話註釋了就好了,默認是HTTP/1.1 gzip_http_version 1.0; #壓縮級別,1-9,數字越大壓縮的越好,時間也越長,看心情隨便改吧 gzip_comp_level 6; #設置須要壓縮的MIME類型,非設置值不進行壓縮 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #跟Squid等緩存服務有關,on的話會在Header裏增長"Vary: Accept-Encoding" gzip_vary off; #IE6對Gzip不怎麼友好,不給它Gzip了 gzip_disable "MSIE [1-6]\.";
4.從新加載Nginx;
linux
<span style="white-space:pre"> </span>在linux中重啓:/usr/local/nginx/sbin/nginx -s reload 在windows中重啓:C:\server\nginx-1.0.2>nginx.exe -s reload
(1).頁面成功壓縮nginx
<span style="white-space:pre"> </span>curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014" HTTP/1.1 200 OK Server: nginx/1.9.1 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(2).css文件成功壓縮
curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014/index.css" HTTP/1.1 200 OK Server: nginx/1.9.1 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
curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/1_mr_smile2014.jpg" HTTP/1.1 200 OK Server: nginx/1.9.1 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://wwww.xxxx.com/mr_smile2014/js/jquery/jquery.js" HTTP/1.1 200 OK Server: nginx/1.9.1 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