1. Compare HTTP output with HTTP output without compression 2. Enable gzip by using apachemod_deflatemodule. Enable gzip. Disable. dapache2restart: Disable. dapache2restart3. Set gzip compression output
1. use compression and HTTP output without compression 2. how to enable gzip using apache mod_deflate module: sudo a2enmod deflatesudo/etc/init. d/apache2 restart: sudo a2dismod deflatesudo/etc/init. d/apache2 restart 3. set gzip compression output
1. Comparison between HTTP output with and without compression
2. Enable gzip
Use apache mod_deflate module to enable gzip
Enabling method:
sudo a2enmod deflatesudo /etc/init.d/apache2 restart
Close method:
sudo a2dismod deflatesudo /etc/init.d/apache2 restart
3. Set the type of gzip compression output
The json output type is application/json.
In httpd. conf Join
AddOutputFilterByType DEFLATE application/json
<?php$data = array( array('name'=>'one','value'=>1), array('name'=>'two','value'=>2), array('name'=>'three','value'=>3), array('name'=>'four','value'=>4), array('name'=>'five','value'=>5), array('name'=>'six','value'=>6), array('name'=>'seven','value'=>7), array('name'=>'eight','value'=>8), array('name'=>'nine','value'=>9), array('name'=>'ten','value'=>10),);header('content-type:application/json');echo json_encode($data);?>
Set the output before gzip:
Output after setting gzip:
4. Use gzip to compress the output of a single json File
After AddOutputFilterByType DEFLATE application/json is set, gzip is used to compress all data output in json format.
If you only want to use gzip to compress the output of a json file, you can use ob_start.
First, you do not need to set AddOutputFilterByType, and then add ob_start ('ob _ gzhandler') at the beginning of the Code ');
<?phpob_start('ob_gzhandler');$data = array( array('name'=>'one','value'=>1), array('name'=>'two','value'=>2), array('name'=>'three','value'=>3), array('name'=>'four','value'=>4), array('name'=>'five','value'=>5), array('name'=>'six','value'=>6), array('name'=>'seven','value'=>7), array('name'=>'eight','value'=>8), array('name'=>'nine','value'=>9), array('name'=>'ten','value'=>10),);header('content-type:application/json');echo json_encode($data);?>