简体   繁体   English

如何启用 gzip?

[英]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.我找到了一些关于如何启用 gzip 的教程,但似乎没有什么对我有用,所以我的问题是如何启用 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?我在一个共享的 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

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 possible to have that file gzipped, It is not being transfered compressed, so is ther any way to do so?但这是我的第一个问题,其次,我有保管箱,托管一个 javscript 文件,我想知道是否可以将该文件 gzip 压缩,它没有被压缩传输,所以有什么办法吗?

Have you tried with ob_gzhandler ?你试过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:作为替代方案,使用 Apache Web 服务器,您可以将DEFLATE 输出过滤器添加到顶级服务器配置或.htaccess文件中:

<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.提示:有时检测 Web 服务器是否正在发送压缩内容非常棘手。 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.在我的 Web 浏览器中使用开发人员工具,我测试了一个压缩和不压缩的 PHP 文件以比较大小。 In my case, the difference was 1 MB (non-compressed) and 56 KB compressed.就我而言,差异是 1 MB(未压缩)和 56 KB 压缩。

All I had to do to enable the encoding at the Apache level is为了在 Apache 级别启用编码,我所要做的就是

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()您也可以在ob_start()之前的 PHP 文件中执行此操作

ini_set("zlib.output_compression", 1);

And to make Apache compress the static resources (eg: .js files , .css files) do as Kamlesh did in his answer并让 Apache 压缩静态资源(例如:.js 文件、.css 文件),就像Kamlesh在他的回答中所做的那样

In the official wiki of Dreamhost they enable this by modifying an htaccess :在 Dreamhost 的官方 wiki 中,他们通过修改 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.这基本上检查是否找到了 mod_czip.c,如果找到了,它将为您压缩文件,以便更快地将它们发送到浏览器。 This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.据说这将下载时间加快了 35-40%,然后文件大小应该下降到 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.通过在 Google 上快速搜索,您可以在 Stackoverflow 上找到另一个线程,并在第三方站点中解决此问题。

In Apache, enabling output compression is fairly straightforward.在 Apache 中,启用输出压缩相当简单。 Add the following to your .htaccess file:将以下内容添加到您的 .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>

Source : http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/来源: 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. ## 首先,您必须更改 WHM(服务器)设置以启用 Gzip。 After that make changes in cPanel setting ##之后更改 cPanel 设置##

  1. For WHM setting => Easy Apache => Exhaustive Options List < here enable - deflate tab >对于 WHM 设置 => Easy Apache => 详尽的选项列表 < 这里启用 -放气标签 >

  2. For cPanel setting => Sofware/Services => Optimize Website < here choose option what you want >对于 cPanel 设置 => 软件/服务 => 优化网站 < 在这里选择你想要的选项 >

The compression can be done in two ways.压缩可以通过两种方式完成。

Apache actually has two compression options: Apache 实际上有两个压缩选项:

  • mod_deflate is easier to set up and is standard. mod_deflate更容易设置并且是标准的。
  • mod_gzip seems more powerful: you can pre-compress content. mod_gzip似乎更强大:您可以预压缩内容。

Deflate is quick and works, so I use it; Deflate 快速且有效,所以我使用它; use mod_gzip if that floats your boat.如果它能让你的船漂浮,请使用 mod_gzip。 In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file.无论哪种情况,Apache 都会检查浏览器是否发送了“Accept-encoding”标头并返回文件的压缩版本或常规版本。 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.如果您无法更改 .htaccess 文件,则可以使用 PHP 返回压缩内容。 Give your HTML file a .php extension and add this code to the top:为您的 HTML 文件添加一个 .php 扩展名并将此代码添加到顶部:

In PHP:在 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).我们检查“Accept-encoding”标头并返回文件的压缩版本(否则为常规版本)。 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.但实际上,如果您能提供帮助,请尝试使用 Apache 来压缩您的输出。 You don't want to monkey with your files.您不想胡乱处理您的文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM