2015年10月8日 星期四

網頁GZIP壓縮設定


 開發網頁時,若碰到內容較多的表單類或是資料表格(Data table or Data grid),常會讓網頁本身的文字內容就很肥大。

尤其若碰到需要引用一些javascript liberaryextjs or flex這種本身就很肥的東西,常常光基本結構還沒有放入任何內容就已經超過2-3MB了,開發測試時可能還感覺不出來,因為大部分都會在本地端開發,但是只要放在正式環境時就會看的出來影響了。

壓縮HTTP內容(HTTP Compression)


所以如果可以將資料壓縮後再傳送,就可以節省許多頻寬在傳輸上了。如下圖示可看的出來減少了網路所傳輸的資料量,但相對的會增加Server壓縮檔案及Browser解壓縮的消耗。



壓縮的效益


壓縮及解壓縮會消耗ServerBrowserCPU,但設定得宜的話,可以大大減少User等待網路的時間,因此視網站的資料情況設定合適的參數是很重要的。

而影音、圖片及壓縮檔案因大部分已經經過壓縮處理,因此不適合再透過Server做壓縮,會導致增加壓縮成本但網路傳輸量卻減少的有限的反效果。

建議只設定三種格式做壓縮,HTMLCSSJavascript。若透過JSON格式取大量資料的話,也可另外增加application/json的設定將其補上。

Tomcat GZIP Compression設定


找到${tomcat_home} /conf/server.xml,修改Connector的設定

<Connector port="8080" maxHttpHeaderSize="8192"
           maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           connectionTimeout="20000" disableUploadTimeout="true"
           compression="on"
          
compressionMinSize="2048"
          
noCompressionUserAgents="gozilla, traviata"
          
compressableMimeType="text/html
, text/css, text/xml"/>

屬性說明(Copy From Apache)
compression
The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off".
Note: There is a tradeoff between using compression (saving your bandwidth) and using the sendfile feature (saving your CPU cycles). If the connector supports the sendfile feature, e.g. the NIO connector, using sendfile will take precedence over compression. The symptoms will be that static files greater that 48 Kb will be sent uncompressed. You can turn off sendfile by setting useSendfile attribute of the connector, as documented below, or change the sendfile usage threshold in the configuration of the DefaultServletin the default conf/web.xml or in the web.xml of your web application.
compressableMimeType
The value is a comma separated list of MIME types for which HTTP compression may be used. The default value istext/html,text/xml,text/plain,text/css,text/javascript,application/javascript .
compressionMinSize
If compression is set to "on" then this attribute may be used to specify the minimum amount of data before the output is compressed. If not specified, this attribute is defaults to "2048".
noCompressionUserAgents
The value is a regular expression (using java.util.regex) matching the user-agentheader of HTTP clients for which compression should not be used, because these clients, although they do advertise support for the feature, have a broken implementation. The default value is an empty String (regexp matching disabled).
  

Weblogic GZIP Compression設定

2.      把下載的jar放到${web_appliaction_home}/WEB-INF/lib
3.      開啟${web_appliaction_home}/WEB-INF/web.xml加入filter的設定
<filter>
    <filter-name>CompressingFilter</filter-name>
    <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
    <init-param>
        <param-name>includeContentTypes</param-name>
        <param-value>text/html,text/css,application/x-javascript</param-value>
    </init-param>
    <init-param>
        <param-name>compressionThreshold</param-name>
        <param-value>256</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CompressingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

參考資料:

沒有留言:

張貼留言