服务器怎么使用gzip压缩_使用gzip压缩服务器端的站点资产

服务器怎么使用gzip压缩

Most everyone is familiar with the .zip extension, the ubiquitous format that archives and compresses files for storage and transmission. Relatively few are aware of the server-side equivalents of gzip and deflate, collectively referred to as “HTTP compression”. Using gzip on your site can reduce web page sizes by up to 70%, creating faster page load times while reducing bandwidth costs, with almost no downside.

大多数人都熟悉.zip扩展名,这是一种无处不在的格式,用于存档和压缩文件以进行存储和传输。 相对很少有人知道gzipdeflate的服务器端等效项,统称为“ HTTP压缩”。 在您的网站上使用gzip可以将网页大小减少多达70%,从而可以缩短页面加载时间,同时降低带宽成本,而且几乎没有缺点。

Unlike .zip, which is often a manual process, compression and decompression of gzip files is negotiated automatically between the server and browser during the HTTP request process. gzip is one more step in compression: before initiating gzip, you should first have optimized and post-optimized your images and code.

.zip (通常是手动过程)不同,在HTTP请求过程中,服务器和浏览器之间会自动协商gzip文件的压缩和解压缩。 gzip是压缩的又一个步骤 :在启动gzip之前,您应该首先对图像和代码 进行 优化后优化

The good news is that every browser in common use accepts compressed HTTP content, and many servers automatically do so. You can test if your site supports gzip by using a free service such as checkgzipcompression or gzipWTF.

好消息是,每个常用的浏览器都接受压缩的HTTP内容,许多服务器会自动接受。 您可以使用免费服务(例如checkgzipcompressiongzipWTF)测试您的站点是否支持gzip

If that test returns negative, you’ll have to turn on gzip for your server. The easiest way to do so is by creating an .htaccess file with the correct commands, uploading the result to the root folder of your site.

如果该测试返回负值,则必须为服务器打开gzip 。 最简单的方法是使用正确的命令创建.htaccess文件,然后将结果上传到网站的根文件夹。

了解.htaccess (Understanding .htaccess)

.htaccess files control the behavior of the server at a local level: denying a browser access to certain files or folders, modifying and redirecting URLs, or (in this case) changing files. A .htaccess file can be created in a common text editor, such as Notepad, or in a fully-fledged IDE. Before you do so, you should be aware of two things:

.htaccess文件在本地级别控制服务器的行为:拒绝浏览器访问某些文件或文件夹,修改和重定向URL或(在这种情况下)更改文件。 可以在普通的文本编辑器(例如记事本 )中或在成熟的IDE中创建.htaccess文件。 在执行此操作之前,您应该注意两件事:

  • It is very likely that your operating system, be it Windows, Mac OS X or Linux, will automatically hide any .htaccess file you create, as any filename that starts with a period is regarded as a protected system file. This extends to IDEs such as Coda, which may not show the file in folder listings by default. You may have to force a Show hidden files option in order to see the file you created.

    WindowsMac OS X或Linux等操作系统很可能会自动隐藏您创建的任何.htaccess文件,因为任何以句点开头的文件名都被视为受保护的系统文件。 这扩展到了诸如Coda之类的IDE,它们在默认情况下可能不会在文件夹列表中显示该文件。 您可能必须强制使用“ 显示隐藏文件”选项才能查看您创建的文件。

  • .htaccess files are powerful juju: adding a typo may disable access to your entire site, or stop your HTML pages from being delivered correctly to the browser. Work slowly, carefully, and methodically. Ideally, test your .htaccess file on a local development server before deploying it to production.

    .htaccess文件功能强大:键入错误可能会禁用对整个网站的访问,或者使HTML页面无法正确发送到浏览器。 缓慢,仔细,有条理地工作。 理想情况下,在将.htaccess文件部署到生产环境之前,对其进行测试。

通过.htaccess进行HTTP压缩 (HTTP Compression via .htaccess)

First, check that you don’t already have a .htaccess file in your root folder (it’s unlikely, but you don’t want to overwrite a file if it’s already there). Use the existing file, or create a new one, which must have .htaccess exactly as the file name.

首先,检查您的根文件夹中没有.htaccess文件(这不太可能,但是如果文件已经存在,则不希望覆盖它)。 使用现有文件,或创建一个新文件,该文件必须具有与文件名完全相同.htaccess文件。

I’m going to assume you run Apache as a web server, which is the most likely option. In that case, write the following near the top of the .htaccess document:

我假设您将Apache作为Web服务器运行,这是最有可能的选择。 在这种情况下,请将以下内容写在.htaccess文档的顶部附近:

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

Then upload the file to the root folder of your site. Usually, that is the same location as your index page.

然后将文件上传到站点的根文件夹。 通常,该位置与您的索引页相同。

Then test the site for gzip support by using one of the services above. If you don’t see any changes, it’s likely that your hosting service server doesn’t support gzip. It’s possible that it will support mod deflate, a related compression option. In that case, replace the code above with:

然后,使用上述服务之一测试站点对gzip的支持。 如果您没有看到任何更改,则可能是您的托管服务服务器不支持gzip。 它可能会支持mod deflate,一个相关的压缩选项。 在这种情况下,将上面的代码替换为:

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript image/svg image/svg+xml
</ifmodule>

… and try the services again.

…,然后重试服务。

这些命令做什么? (What Do These Commands Do?)

Broadly, both sets of commands seek to achieve the same thing: compressing text, HTML, CSS and JavaScript files on the server and sending these compressed versions to browsers that request them. If the browser doesn’t accept gzip or deflate encoded content (very rare, but still a possibility), the original content is sent instead. Update the original file on the server, and a new compressed version is automatically created.

大致而言,这两组命令都力图实现相同的目的:在服务器上压缩文本, HTMLCSSJavaScript文件,并将这些压缩版本发送给请求它们的浏览器。 如果浏览器接受gzip压缩压缩的内容(非常少见,但仍然可行),则发送原始内容。 更新服务器上的原始文件,并自动创建一个新的压缩版本。

Note that bitmap images and other media are not compressed: adding server-side compression to files that are already well-optimized will often increase the file size, rather than reducing it. files are a notable exception: as they are text-based, they can be further compressed after optimization.

请注意,位图图像和其他媒体压缩:将服务器端压缩添加到已经过优化的文件中通常会增加文件大小,而不是减小文件大小。 文件是一个明显的例外:由于它们是基于文本的,因此可以在优化后进一步压缩。

如果.htaccess失败 (If .htaccess fails)

If both .htaccess options fail, it’s likely that your site is hosted on a server that doesn’t support gzip or deflate options, or disallows .htaccess files from modifying site content. In that case, I would contact the company and ask them to turn gzip on.

如果两个.htaccess选项均失败,则可能是您的网站托管在不支持gzip或deflate选项的服务器上,或者不允许.htaccess文件修改网站内容。 在这种情况下,我会与该公司联系并请他们打开gzip。

If the answer is still no, there is one other option. It’s not as effective as mod_gzip or mod_deflate, but it’s better than nothing. So long as you’re using , simply add this line to the very start of every page:

如果答案是否定的,则还有另一种选择。 它不如mod_gzipmod_deflate有效,但总比没有好。 只要您使用 ,只需将以下行添加到每个页面的开头

<?php ob_start("ob_gzhandler"); ?>

“At the very start” means just that: the function can fail if there’s a single space before the PHP, although most servers are more forgiving.

“从一开始”就意味着:如果PHP前面只有一个空格,则该功能可能会失败,尽管大多数服务器都可以宽容。

结论 (Conclusion)

Using HTTP compression is a no-lose proposition: the only downside is a slightly increased load on server processing. This cost is more than offset by the savings and advantages to your site.

使用HTTP压缩是不容置疑的主张:唯一的缺点是服务器处理上的负载会稍微增加。 节省的成本和优势可以抵消您的成本。

If you run your own server (either a virtual server or a dedicated machine) you can gain a slightly greater advantage by placing the gzip commands in your apache httpd.conf file instead. As its name suggests, the .htaccess file is consulted with every HTTP request to your site; commands written in httpd.conf are read just once on server startup, and offer better performance. I’ll detail httpd.conf options in a future article.

如果运行自己的服务器(虚拟服务器或专用计算机),则可以通过将gzip命令放在apache httpd.conf文件中来获得更大的优势。 顾名思义,对您站点的每个HTTP请求都将查阅.htaccess文件。 用httpd.conf编写的命令在服务器启动时仅读取一次,因此性能更好。 我将在以后的文章中详细介绍httpd.conf选项。

翻译自: https://thenewcode.com/847/Compress-Site-Assets-Server-Side-With-gzip

服务器怎么使用gzip压缩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值