开发者

How to enable gzip?

I have found a couple of tutorials on how to enable gzip, but nothing seems to be working for me, so my question is how do i enable gzip. I am on a shared Dreamhost hosting server, It is running PHP version 5.2, and Apache, from the php info i have found this line, maybe this could help?

zlib

ZLib Support    enabled
Stream Wrapper support  compress.zlib://
Stream Filter support   zlib.inflate, zlib.deflate
Compiled Version    1.2.3.3
Linked Version  1.2.3.3

Directive   Local Value Master Value
zlib.output_compression Off Off
zlib.output_compression_level   -1  -1
zlib.output_handler no value    no value

I have also found this line

_SERVER["HTTP_ACCEPT_ENCODING"] gzip, deflate

I don't know if that has anything to do with it. But that is my first question, secondly, i have dropbox, hosting a javscript file, and I am wondering is it pos开发者_如何学运维sible to have that file gzipped, It is not being transfered compressed, so is ther any way to do so?


Have you tried with ob_gzhandler?

<?php ob_start("ob_gzhandler"); ?>
<html>
  <body>
    <p>This should be a compressed page.</p>
  </html>
<body>

As an alternative, with the Apache web server, you can add a DEFLATE output filter to your top-level server configuration, or to a .htaccess file:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml \
        text/css application/x-javascript application/javascript
</IfModule>

Tip: Sometimes it is pretty tricky to detect if the web server is sending compressed content or not. This online tool can help with that.

Using the developer tools in my web browser, I tested a PHP file with and without compression to compare the size. In my case, the difference was 1 MB (non-compressed) and 56 KB compressed.


All I had to do to enable the encoding at the Apache level is

zlib.output_compression = 1 // the PHP.ini file

this will make the server do the necessary request header check, compress, send related headers

you can also do that in your PHP files before the ob_start()

ini_set("zlib.output_compression", 1);

And to make Apache compress the static resources (e.g: .js files , .css files) do as Kamlesh did in his answer


In the official wiki of Dreamhost they enable this by modifying an htaccess:

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

This basically checks to see if mod_czip.c is found and if it is it will compress the files for you so they are faster to send to the browser. This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.

With a quick search on Google you can come up with another thread on Stackoverflow an in a third party site addressing this issue.


In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

Source : http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/


## First of all you have to make changes in WHM (server) setting to enable Gzip. After that make changes in cPanel setting ##

  1. For WHM setting => Easy Apache => Exhaustive Options List < here enable - deflate tab >

  2. For cPanel setting => Sofware/Services => Optimize Website < here choose option what you want >


The compression can be done in two ways.

Apache actually has two compression options:

  • mod_deflate is easier to set up and is standard.
  • mod_gzip seems more powerful: you can pre-compress content.

Deflate is quick and works, so I use it; use mod_gzip if that floats your boat. In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file. However, some older browsers may have trouble (more below) and there are special directives you can add to correct this.

If you can’t change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:

In PHP:

<?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’))
ob_start(“ob_gzhandler”); else ob_start(); ?>

We check the “Accept-encoding” header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don’t want to monkey with your files.

Reference: http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜