Home php教程 php手册 IIS6.0 开启Gzip与PHP Gzip

IIS6.0 开启Gzip与PHP Gzip

Jun 13, 2016 am 09:34 AM
iis php web

因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理。我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下。

IIS 的 GZIP 压缩                                                                        

1.首先备份 IIS 的配置文件, 复制 C:\Windows\system32\inetsrv\metabase.xml到另外的备份文件夹中.   C:\Windows\system32\inetsrv\metabase.xml是 IIS 的核心配置文件,该文件的完整性一但被破坏,IIS 将无法正常运行,严重到需要重新安装系统.

2. 在开始菜单中启动 Internet 信息服务(IIS)管理器,右键点击“网站”属性,打开“服务”选项卡,勾选“HTTP 压缩”的两个选项。“临时目录”和“临时目录最大容量”可根据需要自行设置。设置完成后点击确定。

3. 右键点击“网站”下方的 “Web服务扩展”,添加一个新的Web服务扩展,扩展名填写为“HTTP Compression”或其他,都可以。“要求的文件”添加:c:\windows\system32\inetsrv\gzip.dll ,并勾选“设置扩展状态为允许”,完成后点击确定。 

4.下面的步骤有些复杂,如果没有确定的把握能理解,最好不要尝试,右键点击“Internet 信息服务的”“本地计算机”属性,勾选“允许直接编辑配置数据库”并确定。

5. 在开始菜单中运行 notepad C:\Windows\system32\inetsrv\metabase.xml ,打开metabase.xml 文件,请在任何改动前再次确认该文件已经备份。 

6. 用文本编辑器打开metabase.xml ,用查找功能找到“IIsCompressionScheme”,共有3处,都在一起,分别是deflate、gzip和Parameters,deflate也是一种压缩格式,不过性能上不如gzip。需要修改的是deflate和gzip这两段,参数基本一样,都要修改。

HcDynamicCompressionLevel是用来设置压缩率,默认是0,最高是10。低压缩级别生成稍大一些的压缩文件,但对 CPU 和内存资源的总体影响较小。高压缩级别通常会生成较小的压缩文件,但会占用较多的 CPU 时间和内存。有人说设置成9性价比最高。

HcFileExtensions是用来设置压缩的静态文件扩展名,默认是htm、html、txt,根据网站的自身情况添加扩展名,最基本的是js、css。添加时注意原有的换行格式。

HcScriptFileExtensions是用来设置压缩的动态文件扩展名,默认是asp、dll和exe,根据需要自行添加扩展名,无外乎aspx、php等。可以将这一项的默认删除留空,这样所有的动态响应都以压缩方式发送。另外,还将动态压缩 HcFileExtensions 中所有未指定的静态文件类型,并且因此不将它们放入缓存。同样地,必须将 HcDoDynamicCompression 设置为 true,以进行动态压缩。

PHP Gzip                                                                                

<span>/*</span><span>
* 压缩数据
</span><span>*/</span>
<span>public</span> <span>static</span> <span>function</span> ob_gzip(<span>$content</span>) <span>//</span><span> $content 就是要压缩的页面内容,或者说饼干原料</span>
<span>{   
    </span><span>if</span>(    !<span>headers_sent</span>() && <span>//</span><span> 如果页面头部信息还没有输出</span>
        <span>extension_loaded</span>("zlib") && <span>//</span><span> 而且zlib扩展已经加载到PHP中</span>
        <span>strstr</span>(<span>$_SERVER</span>["HTTP_ACCEPT_ENCODING"],"gzip")) <span>//</span><span>而且浏览器说它可以接受GZIP的页面 </span>
<span>    {
        </span><span>$content</span> = <span>gzencode</span>(<span>$content</span>." OK",9); <span>//</span><span>此页已压缩&rdquo;的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。
                
        //然后用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了!</span>
        <span>header</span>("Content-Encoding: gzip"<span>); 
        </span><span>header</span>("Vary: Accept-Encoding"<span>);
        </span><span>header</span>("Content-Length: ".<span>strlen</span>(<span>$content</span><span>));
    }
        </span><span>return</span> <span>$content</span>; <span>//</span><span>返回压缩的内容,或者说把压缩好的饼干送回工作台。</span>
}
Copy after login

 

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles