php 如何启用gzip?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4709076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:05:11  来源:igfitidea点击:

How to enable gzip?

phpapache.htaccessgzip

提问by mcbeav

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?

我找到了一些关于如何启用 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

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?

不知道跟这个有没有关系。但这是我的第一个问题,其次,我有 Dropbox,托管一个 javscript 文件,我想知道是否可以将该文件压缩,它没有被压缩,那么有什么办法可以做到这一点?

回答by magallanes

Have you tried with ob_gzhandler?

你试过 ob_gzhandler 吗?

php manual

php手册

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

Tip: Sometimes is pretty tricky to detect if the web is send compressed or not, i use firefox's firebug plugin, i tested a php file without compression and with compression and compare the size, in my case, the difference was 1mb (non compressed) and 56kb compressed.

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

Or in your .htaccess

或者在您的 .htaccess 中

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

回答by Accountant ?

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()

你也可以在你的 PHP 文件中这样做 ob_start()

ini_set("zlib.output_compression", 1);

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

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

回答by pablasso

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. This supposedly speeds up download times 35-40%, and then the file size should supposedly go down to 55-65%.

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

With a quick search on Google you can come up with another thread on Stackoverflowan in a third partysite addressing this issue.

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

回答by Kamlesh

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

在 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>

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/

回答by vinod

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

## 首先,您必须更改 WHM(服务器)设置以启用 Gzip。之后更改 cPanel 设置##

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

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

  1. 对于 WHM 设置 => Easy Apache => 详尽选项列表 < 此处启用 - deflate选项卡 >

  2. 对于 cPanel 设置 => 软件/服务 => 优化网站 < 这里选择你想要的选项 >

回答by Avi Tyagi

The compression can be done in two ways.

可以通过两种方式进行压缩。

Apache actually has two compression options:

  • mod_deflateis easier to set up and is standard.
  • mod_gzipseems 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.

Apache 实际上有两个压缩选项:

  • mod_deflate更容易设置并且是标准的。
  • mod_gzip似乎更强大:您可以预压缩内容。

Deflate 快速且有效,所以我使用它;如果它使您的船漂浮,请使用 mod_gzip。无论哪种情况,Apache 都会检查浏览器是否发送了“Accept-encoding”标头并返回文件的压缩版本或常规版本。但是,一些较旧的浏览器可能会遇到问题(更多内容见下文),您可以添加特殊指令来纠正此问题。

如果您无法更改 .htaccess 文件,则可以使用 PHP 返回压缩内容。为您的 HTML 文件添加一个 .php 扩展名并将此代码添加到顶部:

在 PHP 中:

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

我们检查“Accept-encoding”标头并返回文件的 gzip 版本(否则为常规版本)。这几乎就像构建自己的网络服务器(多有趣!)。但实际上,如果可以的话,请尝试使用 Apache 来压缩您的输出。你不想乱搞你的文件。

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/