如何启用gzip?

mcb*_*eav 29 php apache .htaccess gzip

我找到了几个关于如何启用gzip的教程,但似乎没有什么对我有用,所以我的问题是如何启用gzip.我在一个共享的Dreamhost托管服务器,它运行PHP版本5.2,和Apache,从我发现此行的PHP信息,也许这可以帮助?

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
Run Code Online (Sandbox Code Playgroud)

我也找到了这条线

_SERVER["HTTP_ACCEPT_ENCODING"] gzip, deflate
Run Code Online (Sandbox Code Playgroud)

我不知道这与它有什么关系.但这是我的第一个问题,其次,我有dropbox,托管一个javscript文件,我想知道是否可以将该文件gzipped,它没有被转移压缩,所以还有什么方法可以这样做?

mag*_*nes 50

你试过ob_gzhandler吗?

php手册

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

提示:有时检测网络是否发送压缩是非常棘手的,我使用firefox的firebug插件,我测试了一个没有压缩和压缩的php文件并比较了大小,在我的情况下,差异是1mb(非压缩)和56kb压缩.

或者在你的.htaccess中

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css         application/x-javascript application/javascript
</IfModule>
Run Code Online (Sandbox Code Playgroud)

  • 确保检查浏览器是否支持gzip压缩:<?php if(substr_count($ _ SERVER ['HTTP_ACCEPT_ENCODING'],'gzip')){ob_start("ob_gzhandler");}?> (4认同)

Acc*_*t م 8

在Apache级别启用编码所需要做的就是

zlib.output_compression = 1 // the PHP.ini file
Run Code Online (Sandbox Code Playgroud)

这将使服务器执行必要的请求标头检查,压缩,发送相关标头

您还可以在PHP文件之前执行此操作 ob_start()

ini_set("zlib.output_compression", 1);
Run Code Online (Sandbox Code Playgroud)

并使Apache压缩静态资源(例如.js文件,.css文件)就像Kamlesh在他的回答中所做的那样


pab*_*sso 6

在Dreamhost的官方维基中,他们通过修改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>
Run Code Online (Sandbox Code Playgroud)

这基本上检查是否找到mod_czip.c,如果是,它将为您压缩文件,以便更快地发送到浏览器.据说这可以加快下载时间35-40%,然后文件大小应该降低到55-65%.

通过在Google上快速搜索,您可以在Stackoverflow上找到另一个线程,第三方网站中解决此问题.


Kam*_*esh 5

在Apache中,启用输出压缩非常简单。将以下内容添加到您的.htaccess文件中:

# 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>
Run Code Online (Sandbox Code Playgroud)

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