如何在 CentOS 7 上使用 gzip 模块配置 Nginx 进行压缩


在这篇文章中,我们配置了已安装在 CentOS 7 上的 Nginx,使其使用 gzip 压缩来为客户端提供服务,从而减小发送给网站访问者的内容大小。我们可以通过减小网站传输的文件大小来配置网站加载速度,这取决于 Web 浏览器下载的所有文件的大小,从而加快加载速度,并降低带宽使用成本。**gzip** 是一个流行的数据压缩包,我们将使用 gzip 配置 Nginx 来压缩服务器上的文件,以便更快地加载文件。

创建用于压缩的测试文件

在这里,我们将在默认的 Nginx 目录中创建一些演示文件来测试 gzip 压缩。Nginx 然后将检查文件的内容,确定 MIME 类型并确定文件用途。

由于 Nginx 不会压缩非常小的文件,因此我们将创建大小约为 1KB 的文件来验证 Nginx 是否在应该压缩的地方进行了压缩。我们使用 truncate 命令在默认的 Nginx 目录中创建一个名为 demo.html 的 1KB 文件。

扩展名表示这是一个 HTML 文件。

$ sudo truncate -s 1k /usr/share/nginx/html/demo.html

我们还将使用相同的命令创建更多演示文件:一个 .jpg 图片文件,一个 .css 样式表和一个 .js JavaScript 文件。

$ sudo truncate -s 1k /usr/share/nginx/html/demo.jpg
$ sudo truncate -s 1k /usr/share/nginx/html/demo.css
$ sudo truncate -s 1k /usr/share/nginx/html/demo.js

检查默认浏览器行为

现在,我们将检查 Nginx 在使用我们刚刚创建的文件进行压缩时的行为。我们将检查使用压缩提供的 demo.html。

以下是使用 gzip 压缩从我们的 Nginx 服务器请求文件的命令,我们也可以使用 Accept-Encoding: gzip 传递给 curl 来测试压缩。

$ curl -H "Accept-Encoding: gzip" -I https:///demo.html
Output:
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 18:53:06 GMT
Content-Type: text/html
Content-Length: 1024
Last-Modified: Fri, 11 July 2016 18:48:02 GMT
Connection: keep-alive
ETag: "56eg2be82-400"
Accept-Ranges: bytes

在 Nginx 配置中,默认情况下 gzip 压缩被禁用,因此我们在上面的输出中没有看到 Content-Encoding: gzip。现在,我们将以相同的方式测试名为 demo.jpg 的图像,查看 Nginx 如何压缩图像。

$ curl -H "Accept-Encoding: gzip" -I https:///demo.jpg
Output:
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 18:58:43 GMT
Content-Type: image/jpeg
Content-Length: 1024
Last-Modified: Fri, 11 July 2016 18:48:35 GMT
Connection: keep-alive
ETag: "563e2be85-400"
Accept-Ranges: bytes

这里的输出也没有压缩。

我们还将测试 CSS 样式表。

$ curl -H "Accept-Encoding: gzip" -I https:///demo.css
Output:
Nginx response headers for CSS file
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 16:59:34 GMT
Content-Type: text/css
Content-Length: 1024
Last-Modified: Fri, 11 July 2016 12:48:55 GMT
Connection: keep-alive
ETag: "56e2be85-400"
Accept-Ranges: bytes

启用并配置 Nginx 使用 gzip 压缩模块

默认情况下,当我们安装 Nginx 时,gzip 压缩模块已安装,但在 Nginx gzip 模块中未启用。我们将在 /etc/nginx/conf.d 目录中创建一个配置文件,这些文件在我们启动 Nginx 服务时会自动加载。

$ sudo vi /etc/nginx/conf.d/gzip.conf
Files contents:
##
# `gzip` compresstion enableing Settings
#
#
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

上述配置文件中使用的配置设置。

gzip on - directive enables the Gzip compression.
gzip_disable "msie6" - excludes Internet Explorer 6 from the browsers that will receive compressed files, because IE6 does not support gzip at all.

gzip_vary and gzip_proxied - settings make sure that proxy servers between the browser and the server will recognize compression correctly.
gzip_comp_level 6 - sets how much files will be compressed. The higher the number, the higher the compression level and the resources usage. 6 is a reasonable middle ground.

gzip_http_version 1.1 - is used to limit gzip compression to browsers supporting the HTTP/1.1 protocol. If the browser does not support it, there is a good chance it does not support gzip either.
gzip_min_length 256 - tells Nginx not to compress files smaller than 256 bytes. Very small files barely benefit from compression.
gzip_types lists all - of the MIME types that will be compressed. In this case, the list includes HTML pages, CSS stylesheets, Javascript and JSON files, XML files, icons, SVG images, and web fonts.

重新启动 Nginx 以应用配置

$ sudo systemctl restart nginx

验证新的配置

我们将使用命令提示符中的 curl 命令测试 .html 文件,以检查压缩是否已启用。

$ curl -H "Accept-Encoding: gzip" -I https:///demo.html
Output:
Nginx response headers
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 17:19:46 GMT
Content-Type: text/html
Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip

我们也可以对其余的 .jpg、.css 和 .js 文件进行相同的测试。

$ curl -H "Accept-Encoding: gzip" -I https:///demo.jpg
Output:
Nginx response headers
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 17:19:46 GMT
Content-Type: image/jpeg
Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip
$ curl -H "Accept-Encoding: gzip" -I https:///demo.css
Output:
Nginx response headers
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 17:19:46 GMT
Content-Type: text/css
Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip
$ curl -H "Accept-Encoding: gzip" -I https:///demo.js
Output:
Nginx response headers
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 11 July 2016 17:19:46 GMT
Content-Type: text/js
Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip

通过在配置文件中进行简单的更改,将 gzip 压缩模块添加到 Nginx Web 浏览器,我们可以加快网站加载速度,这也有利于带宽消耗,并使带宽有限的访问者能够更快地访问网站。

更新于:2019年10月18日

409 次浏览

开启您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.