如何在 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 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告