如何驗證Tomcat Gzip配置是否生效的方法
我們在使用Tomcat優(yōu)化配置時,都會開始Tomcat的Gzip壓縮功能,配置如下:
<Connector port="9080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg"
/>
即把以下內(nèi)容加入到 apache-tomcat-6\conf\server.xml 中的connector標(biāo)簽中
<!-- Note : To use Gzip compression you could set the following properties :
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg"
-->
參數(shù)說明:
compression="on" 打開壓縮功能
compressionMinSize="2048" 啟用壓縮的輸出內(nèi)容大小,當(dāng)被壓縮對象的大小>=該值時才會被壓縮,這里面默認(rèn)為2KB
noCompressionUserAgents="gozilla, traviata" 對于以下的瀏覽器,不啟用壓縮
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg" 壓縮類型
注意:tomcat7以后,js文件的mimetype類型變?yōu)榱薬pplication/JavaScript,具體的tomcat7定義的類型可以在:conf/web.xml文件中找到。
我的Tomcat6所以 還是 text/javascript
Tomcat7 js文件的mimetype類型變?yōu)榱薬pplication/javascript
自己注意 配錯了起不到壓縮的作用哦
那么我們?nèi)绾螠y試配置的Gzip壓縮功能生效了呢?
答案就是:使用apache HttpClient訪問該tomcat加載項目中的一個靜態(tài)資源(比如一個js文件),然后打印請求的資源內(nèi)容 或 資源ContentLength,如果打印的資源內(nèi)容為亂碼 或 ContentLength為 -1,則說明gzip生效了。
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* @ClassName: GzipTest.java
* @Description: TODO(用一句話描述該文件做什么)
*
* @author Administrator
* @E-mail 809044093@qq.com
* @version V1.0
* @Date 2014-3-27 上午09:07:00
*/
public class GzipTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
HttpClient http = new HttpClient();
GetMethod get = new GetMethod("http://127.0.0.1:9080/membercms/style/shop/js/base.js");
try{
get.addRequestHeader("accept-encoding", "gzip,deflate");
get.addRequestHeader("user-agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");
int er = http.executeMethod(get);
if(er==200){
System.out.println(get.getResponseContentLength());
String html = get.getResponseBodyAsString();
System.out.println(html);
System.out.println(html.getBytes().length);
}
}finally{
get.releaseConnection();
}
}
}
控制臺結(jié)果為亂碼
說明配置壓縮網(wǎng)站的信息成功,此法可能對服務(wù)器cpu有些損耗。
Apache 開啟Gzip壓縮配置:
去掉以下兩個 注釋#
LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so
在 httpd.conf最后加入
#apache Gzip <Location /> # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|doc|avi|mov|mp3|rm)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/js # Make sure proxies don't deliver the wrong content #Header append Vary User-Agent env=!dont-vary </Location>
設(shè)置完成之后 訪問http://tool.chinaz.com/Gzips/ 輸入你的網(wǎng)站域名 檢測壓縮情況。
相關(guān)文章
Maven使用tomcat8-maven-plugin插件的詳細教程
這篇文章主要介紹了Maven使用tomcat8-maven-plugin插件的詳細教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Tomcat中catalina.bat設(shè)置為UTF-8控制臺出現(xiàn)亂碼
這篇文章主要介紹了Tomcat中catalina.bat設(shè)置為UTF-8控制臺出現(xiàn)亂碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Tomcat配置訪問日志和線程數(shù)的實現(xiàn)步驟
本文主要介紹了Tomcat配置訪問日志和線程數(shù)的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
如何通過LambdaProbe實現(xiàn)監(jiān)控Tomcat
這篇文章主要介紹了如何通過LambdaProbe實現(xiàn)監(jiān)控Tomcat,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10

