First scenario: Add in. htaccess
The code is as follows |
Copy Code |
AddHandler application/x-httpd-php. css. html. js #这是添加你要压缩的类型 Php_value auto_prepend_file "/home/a4284418/public_html/gzip.php" |
#这是你给这些类型增加一个自动运行的php代码, the absolute address of your space must be filled in the back
gzip.php
The code is as follows |
Copy Code |
<?php Ob_start ("Ob_gzhandler"); Ob_start ("compress"); If CSS does not explain, only display pages, please add the following red code, remove the annotation character /* $pathinfo = PathInfo ($_server[php_self]); Switch ($pathinfo [' extension ']) { Case "CSS": Header ("Content-type:text/css"); Break Case "HTML": Header ("content-type:text/html"); Break Case "JS": Header ("Content-type:text/javascript"); Break Default:break; } */ ?> |
The second scenario: This is an upgraded version of Gzip, which allows you to cache gzip compressed files, avoid duplicate compression, and add in. htaccess
Htaccess rewriterule (. *.css$|. *.js$) gzip.php?$1 [L]
The code is as follows |
Copy Code |
<?php Htaccess rewriterule (. *.css$|. *.js$) gzip.php?$1 [L] Project Root Path Define (' Abspath ', DirName (__file__). /'); GZIP compression switch $cache = true; A directory that stores the GZ files to ensure that they are writable $cachedir = ' gzip-cache/'; if (!is_dir (Abspath $cachedir)) { mkdir (Abspath. $cachedir); } Determine if GZIP is supported $gzip = strstr ($_server[' http_accept_encoding '], ' gzip '); $deflate = strstr ($_server[' http_accept_encoding '], ' deflate '); See if the browser supports gzip or deflate, then none $encoding = $gzip? ' gzip ': ($deflate? ' Deflate ': ' None '); if (!isset ($_server[' query_string ')) exit (); $key =array_shift (Explode ('? ', $_server[' query_string ')); $key =str_replace ('.. /', ', $key); $key =basename ($_server[' query_string ')); Absolute path to File $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 ';//Generate GZ file path $type = "content-type:text/html"; Default type Information $pathInfo = PathInfo ($filename); Determine file type information based on suffix $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)) { If there is a GZ file $mtime = Filemtime ($cache _filename); $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT '; Read GZ file output $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)) { No corresponding GZ file $mtime = Mktime (); $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT '; $content = file_get_contents ($filename);//Read file $content = Gzencode ($content, 9, $gzip? force_gzip:force_deflate);/Compressed file contents 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 ')) { Write to GZ file for next use Fwrite ($fp, $content); Fclose ($FP); } Echo $content; }else{ Header ("http/1.0 404 Not Found"); } }else {//processing does not use output in gzip mode. Principle Basic Ditto 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"); } } ?> |
The third scenario: directly modify the server environment
1,httpd.conf modification
LoadModule headers_module modules/mod_headers.so Open
LoadModule Deflate_module modules/mod_deflate.so Add
2,apache2.2\conf\extra\httpd-vhosts.conf default virtual Host in Riga
The code is as follows |
Copy Code |
<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> Example: <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 Compression
The Nginx Configuration gzip section is as follows:
The code is as follows |
Copy Code |
gzip on; Gzip_min_length 1k; Gzip_buffers 64k; Gzip_http_version 1.1; Gzip_comp_level 6; Gzip_types text/plain application/x-javascript text/css application/xml; Gzip_vary on; |
The Nginx compression output has a set of gzip compression instructions to implement. The instructions are located between two curly braces in http{.
gzip on;
This directive is used to turn the Gzip module on or off (On/off)
Gzip_min_length 1k;
Sets the minimum number of bytes of pages allowed to compress, and the number of page bytes is obtained from the header content-length. The default value is 0, regardless of how much of the page is compressed. It is recommended that you set the number of bytes larger than 1k, less than 1k may be more pressing.
Gzip_buffers 4 16k;
Set up the system to get several units of cache to store the gzip compressed result data stream. 4 16k represents 16k as the installation of the original data size in 16k of 4 times times the application memory.
Gzip_http_version 1.1;
Protocol version to identify HTTP (1.0/1.1)
Gzip_comp_level 2;
gzip compression ratio, 1 compression faster than the minimum processing speed, 9 compression than the largest but the slowest processing speed (faster transmission but more CPU consumption)
Gzip_types text/plain application/x-javascript text/css application/xml
Matches the MIME type for compression, and the "text/html" type is always compressed, regardless of whether it is specified.
Gzip_vary on;
and HTTP headers have a relationship, add a vary head, to the proxy server, and some browsers support compression, some do not support, so avoid wasting unsupported also compression, so according to the client's HTTP headers to determine whether the need to compress
Apache gzip Compression
code is as follows |
copy code |
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> |