Gzip compression is a good way to compress and transfer pages. we often open it on the server, so that a KB file may be less than 20 kB after being compressed by gzip, next I will introduce the gzip compression detection and enabling methods. how to Open gzip... gzip compression is a good way to compress and transfer pages. we often open it on the server, so that a KB file may be less than 20 kB after being compressed by gzip, next I will introduce the gzip compression detection and enabling methods.
How to enable gzip compression?
Step 2 of apache, the code is as follows:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
Add the following code in http. conf:
# Begin gzip # enable gzip compression for apache. you must install the gzip module.
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript application/javascript
# END GZIP
If you are an nginx server, refer to the following code:
gzip on; gzip_min_length 1000; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/text text/html text/plain text/xml text/css application/x-javascript application/javascript;
Restart apache or nginx. how can I check whether gzip is enabled correctly? the following php code can be used:
// Use gzip to compress the webpage. // file_get_contents directly obtains the webpage garbled characters. Header ('content-Type: text/html; charset = utf-8 '); $ url = 'http: // www.phprm.com'; $ file = fopen ($ url, "rb"); // read-only 2 bytes. if the value is (hexadecimal) 1f 8b (10 hexadecimal) 31 139, gzip is enabled; $ bin = fread ($ file, 2); fclose ($ file); $ strInfo = @ unpack ("C2chars", $ bin); $ typeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2 ']); $ isGzip = 0; switch ($ typeCode) {case 31139: // The website has enabled gzip $ isGzip = 1; break; default: $ isGzip = 0;} $ url = $ is Gzip? "Compress. zlib ://". $ url: $ url; // ternary expression $ mierHtml = file_get_contents ($ url); // Obtain Mill military network data $ mierHtml = iconv ("gbk", "UTF-8 ", $ mierHtml); echo $ mierHtml;
Example 2 code:
Article URL:
Reprint ^ at will, but please attach the tutorial address.