gzip是一种网络数据压缩传输技巧,可以让我们的带宽省70%左右,这可是一个非常可观的东西了,下面我来给大家整理几种gzip配置方法吧.
第一种方案:在.htaccess 中添加如下代码:
- AddHandler application/x-httpd-php .css .html .js
-
- #这是添加你要压缩的类型
-
- php_value auto_prepend_file “/home/a4284418/public_html/gzip.php”
-
- #这是你给这些类型增加一个自动运行的php代码,后面一定要填你空间的绝对地址
gzip.php,代码如下:
- <?php
- ob_start(“ob_gzhandler”);
- ob_start(“compress”);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ?>
第二种方案:此种方案是gzip的升级版,可以将gzip的压缩文件缓存下来,避免重复压缩,在.htaccess 中添加:
htaccess RewriteRule (.*.css$|.*.js$) gzip.php?$1 [L],代码如下:
- <?php
-
-
- define(‘ABSPATH’, dirname(__FILE__).’/');
-
- $cache = true;
-
- $cachedir = ‘gzip-cache/’;
- if (!is_dir(ABSPATH.$cachedir))
- {
- mkdir(ABSPATH.$cachedir);
- }
-
- $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’);
- $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], ‘deflate’);
-
- $encoding = $gzip ? ‘gzip’ : ($deflate ? ‘deflate’ : ‘none’);
- if(!isset($_SERVER['QUERY_STRING'])) exit();
-
-
- $key =basename($_SERVER['QUERY_STRING']);
-
- $filename=ABSPATH.$_SERVER['QUERY_STRING'];
- $symbol=’^';
- $rel_path=str_replace(ABSPATH,”,dirname($filename));
- $namespace=str_replace(‘/’,$symbol,$rel_path);
- $cache_filename=ABSPATH.$cachedir.$namespace.$symbol.basename($filename).’.gz’;
- $type=”Content-type: text/html”;
- $pathInfo = pathinfo($filename);
-
- $ext = $pathInfo['extension'];
- switch ($ext){
- case ‘css’:
- $type=”Content-type: text/css”;
- break;
- case ‘js’:
- $type=”Content-type: text/javascript”;
- break;
- default:
- exit();
- }
- if($cache)
- {
- if(file_exists($cache_filename))
- {
-
- $mtime = filemtime($cache_filename);
- $gmt_mtime = gmdate(‘D, d M Y H:i:s’, $mtime) . ‘ GMT’;
-
- $content = file_get_contents($cache_filename);
- header(“Last-Modified:” . $gmt_mtime);
- header(“Expires: “);
- header(“Cache-Control: “);
- header(“Pragma: “);
- header($type);
- header(“Tips: Normal Respond (Gzip)”);
- header(“Content-Encoding: gzip”);
- echo $content;
- }else if(file_exists($filename))
- {
-
- $mtime = mktime();
- $gmt_mtime = gmdate(‘D, d M Y H:i:s’, $mtime) . ‘ GMT’;
- $content = file_get_contents($filename);
- $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
- header(“Last-Modified:” . $gmt_mtime);
- header(“Expires: “);
- header(“Cache-Control: “);
- header(“Pragma: “);
- header($type);
- header(“Tips: Build Gzip File (Gzip)”);
- header (“Content-Encoding: ” . $encoding);
- header (‘Content-Length: ‘ . strlen($content));
- if ($fp = fopen($cache_filename, ‘w’))
- {
-
- fwrite($fp, $content);
- fclose($fp);
- }
- echo $content;
-
- }else{
- header(“HTTP/1.0 404 Not Found”);
- }
- }else
- {
- if(file_exists($filename))
- {
- $mtime = filemtime($filename);
- $gmt_mtime = gmdate(‘D, d M Y H:i:s’, $mtime) . ‘ GMT’;
- header(“Last-Modified:” . $gmt_mtime);
- header(“Expires: “);
- header(“Cache-Control: “);
- header(“Pragma: “);
- header($type);
- header(“Tips: Normal Respond”);
- $content = readfile($filename);
- echo $content;
- }else
- {
- header(“HTTP/1.0 404 Not Found”);
- }
- }
-
- ?>
第三种方案:直接修改服务器环境.
1,httpd.conf修改
LoadModule headers_module modules/mod_headers.so 开启
LoadModule deflate_module modules/mod_deflate.so 添加
2,Apache2.2\conf\extra\httpd-vhosts.conf 的默认虚拟主机里加如下代码:
- <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 127.0.0.3>
- DocumentRoot ‘D:/xampp/htdocs/wmt’
- ServerName 127.0.0.3
- DirectoryIndex index.php index.html
- <Directory “D:\xampp\htdocs\wmt”>
- Options Indexes FollowSymLinks Includes ExecCGI
- AllowOverride All
- Order Allow,Deny
- Allow from all
- </Directory>
- <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>
nginx gzip压缩,nginx 配置gzip段如下:
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 16 64k;
- gzip_http_version 1.1;
- gzip_comp_level 6;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
Nginx的压缩输出有一组gzip压缩指令来实现,相关指令位于http{….}两个大括号之间.
gzip on;:该指令用于开启或关闭gzip模块(on/off)
gzip_min_length 1k;:设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 4 16k;:设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
gzip_http_version 1.1;:识别http的协议版本(1.0/1.1)
gzip_comp_level 2;:gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types text/plain application/x-javascript text/css application/xml:匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的.
gzip_vary on;:和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩.
apache gzip压缩,代码如下:
- LoadModule deflate_module modules/mod_deflate.so
- <IfModule mod_deflate.c>
- SetOutputFilter DEFLATE
- DeflateCompressionLevel 9
- AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
- AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/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>
|