tomcat配置:
<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />
1) compression="on" 打开压缩功能
2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为2KB
3) noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml" 压缩类型
ps:对于某些文本文件比如:log、txt等文件,我们也可以让服务器采用gzip压缩传输,修改conf目录下web.xml,添加
<mime-mapping>
<extension>log</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
等,就可以指定压缩传输了。通常情况下,压缩传输能大幅度提高展示速度。
apache配置:
1)针对Apache2.0之前的版本,它原本是不支持的,不过可以通过添加第三方的module_gzip模 块来启用
2)针对Apache2.0及之后的版本,Apache提供支持, 不过不叫gzip,而叫mod_deflate
下面 就对Apache2.0及之后的版本作一个说明
1) 去掉#LoadModule headers_module modules/mod_headers.so前面的注释#,
2) 添加LoadModule deflate_module modules/mod_deflate.so
3) 在VirtualHost中添加
<Location "/">
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
完整的演示:
# 加载deflate模块
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
<VirtualHost *:80>
DocumentRoot f:/apacheTest
<Location "/">
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
</VirtualHost>
配置日志:
<ifmodule mod_deflate.c>
DeflateCompressionLevel 6
SetOutputFilter DEFLATE
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat ‘"%r" %{outstream}n/%{instream}n (%{ratio}n%%)’ deflate
#CustomLog logs/deflate_log.log deflate
CustomLog "|bin/rotatelogs.exe logs/deflate_log_%Y%m%d%H.log 720 1" deflate
</ifmodule>
配置缓存:
/usr/local/apache111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/metadata/mod_expires.c
/usr/local/apache111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/metadata/mod_headers.c
vi /usr/local/apache111/bin/apr-1-config
改 LDFLAGS="" 为 LDFLAGS="-lz"
/usr/local/apache111/bin/apxs -i -c /home/software/bak/httpd-2.2.3/modules/filters/mod_deflate.c
httpd.conf配置:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule expires_module modules/mod_expires.so
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
ExpiresByType text/css M259200
ExpiresByType application/x-javascript M300
ExpiresByType image/gif "access plus 2 month"
ExpiresByType image/jpeg "access plus 2 month"
ExpiresByType image/png "access plus 2 month"
ExpiresByType image/x-icon "access plus 2 month"
ExpiresByType application/x-shockwave-flash A2592000
</IfModule>
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
# DeflateFilterNote Input instream
# DeflateFilterNote Output outstream
# DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog logs/deflate_log deflate
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|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
</IfModule>
/usr/local/apache111/bin/apachectl -t
测试httpd.conf配置文件