开发者

How to use gzip

开发者 https://www.devze.com 2023-01-05 05:46 出处:网络
Can anyone who is using GZip for HTML, JS, PHP and CSS explain how to im开发者_如何学运维plement this on a server?

Can anyone who is using GZip for HTML, JS, PHP and CSS explain how to im开发者_如何学运维plement this on a server?

What I want to know is:

  1. What do I have to write in .htaccess?
  2. What and from which source do I have to download?
  3. Most important: does it make a difference and does it pay off?


You need mod_deflate or mod_gzip to be enabled on your httpd.conf. After that, you should put the following lines into your configuration or .htaccess file.

Apache 1.3.x:

mod_gzip_on Yes  

mod_gzip_item_include mime ^application/x-javascript$  
mod_gzip_item_include mime ^application/json$  
mod_gzip_item_include mime ^text/.*$  

mod_gzip_item_include file \.html$  
mod_gzip_item_include file \.php$  
mod_gzip_item_include file \.js$  
mod_gzip_item_include file \.css$  
mod_gzip_item_include file \.txt$  
mod_gzip_item_include file \.xml$  
mod_gzip_item_include file \.json$  

Header append Vary Accept-Encoding

Apache 2.0

AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/json   
Header append Vary Accept-Encoding


It does pay off: HTML pages, being clear text, can often be zipped massively, by up to 90 percent.

Gzipping usually needs to be activated in your central server configuration, though, not .htaccess.

If you are on a shared hosting server, chances are it is already installed and turned on.

Use something like Firebug's "Net" tab to find out. Make a request to a page on your server, and click open the file view. If the response headers contain Content-Encoding: gzip, gzipping is already activated.


Does it pay off? There are scenarios, where it will be even slower with compression turned on..

When you turn on compression, Apache needs to wait, till all PHP is completed, before sending any content to the client. With compression turned off, Apache could start sending, with the first echo statement.

Due to this, the client may get the head-section fast, where it gets the info which stylesheets and JS to load and can request them, whil the rest of the PHP script is still generating HTML.

So when your PHP takes time to run, you should most likely not activate compression.

Still there are a lot of positiv effects of compression.

0

精彩评论

暂无评论...
验证码 换一张
取 消