一篇文章讀懂nginx的gzip_static模塊
Nginx支持靜態(tài)和動態(tài)兩種包體gzip壓縮方式,分別對應(yīng)模塊ngx_http_gzip_static,ngx_http_gzip。
我們知道gzip是CPU密集型的應(yīng)用,實時動態(tài)壓縮比較消耗CPU資源。另外,如果使用gzip,則sendfile零拷貝技術(shù)無法使用。為進一步提高Nginx的性能,我們可以使用靜態(tài)gzip壓縮,提前將需要壓縮的文件壓縮好,當(dāng)客服請求到達時,直接發(fā)送壓縮好的.gz文件,如此就減輕了服務(wù)器CPU的壓力,提高了性能。缺省ngx_http_gzip_static模塊并未啟用,需要重新編譯。
#注:根據(jù)需要自行添加其它參數(shù) ./configure --with-http_gzip_static_module
準(zhǔn)備.gz文件:所有待壓縮的文件,需要保留源文件和.gz文件,在相同WEB目錄。如下,以index.html為例。
#壓縮保留源文件的方法: [root@test01 html]# gzip -c index.html > index.html.gz [root@test01 html]# ll index.* -rw-r--r--. 1 root root 620 Jun 23 2021 index.html -rw-r--r--. 1 root root 401 Jun 23 2021 index.html.gz
使用touch同步源文件和.gz文件的修改時間。文件修改時間對應(yīng)Last-Modified響應(yīng)字段,HTTP緩存中使用很廣泛,同步二者時間,目的是保持緩存過期判斷的一致性。
touch index.html.gz -r index.html
添加配置文件:
gzip_static on;
gzip_static優(yōu)先級高于gzip,$gzip_ratio對于gzip_static不生效,如果gzip_static失效,如缺少.gz,則gzip會生效。
gzip_static生效時,和gzip不同,Content-Encoding和Cotent-Length可以同時存在,因為響應(yīng)在發(fā)送前已經(jīng)明確其大小。
實際執(zhí)行的效果:
[root@test01 html]# curl test --compressed -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 04:14:02 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip
也可以考慮用always參數(shù)
gzip_static always;
always的語義是不考慮客戶端是否支持gzip解壓【注:依據(jù)是客戶端發(fā)送的Accept-Encoding】,Nginx都將發(fā)送.gz文件,而on則是當(dāng)客戶端不支持gzip解壓時,則發(fā)送原始文件。
下面是gzip_static on,curl啟用壓縮和不啟用壓縮的對比,可以看到僅當(dāng)curl啟用壓縮才發(fā)送.gz文件。
[root@test01 html]# curl test --compressed -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:27:43 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip [root@test01 html]# curl test -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:27:49 GMT Content-Type: text/html Content-Length: 620 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-26c" Accept-Ranges: bytes
下面是設(shè)置為gzip_static always,curl啟用壓縮和不啟用壓縮的對比,可以發(fā)現(xiàn)無論curl是否啟用壓縮,都將發(fā)送.gz文件。
[root@test01 html]# curl test -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Wed, 23 Feb 2022 07:32:56 GMT
Content-Type: text/html
Content-Length: 401
Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT
Connection: keep-alive
ETag: "60d2d558-191"
Content-Encoding: gzip #客戶端沒啟用壓縮,返回的內(nèi)容仍然是gzip壓縮的
[root@test01 html]# curl test --compressed -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Wed, 23 Feb 2022 07:33:05 GMT
Content-Type: text/html
Content-Length: 401
Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT
Connection: keep-alive
ETag: "60d2d558-191"
Content-Encoding: gzip
[root@test01 html]# curl test --compressed
<!DOCTYPE html>
<html lang="en">
<head>
<title>stream ssl test!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>stream ssl test!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a rel="external nofollow" >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#因為沒有啟用壓縮,所以.gz文件無法解壓,將被當(dāng)做二進制文件輸出,有Warning提示。
[root@test01 html]# curl test
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
[root@test01 html]#
Chrome中也可以通過控制Accept-Encoding的發(fā)送,仿真是否需要響應(yīng)的包體壓縮,看下圖:

總之,gzip_static是對gzip的補充,通過簡單的設(shè)置,就能使Nginx提供更好的性能。
參考:gzip_static
相關(guān)文章:一文讀懂nginx gzip
總結(jié)
到此這篇關(guān)于一篇文章讀懂nginx中g(shù)zip_static模塊的文章就介紹到這了,更多相關(guān)nginx gzip_static模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx 配置代理服務(wù)地址最后多加反斜杠和不加反斜杠的區(qū)別小結(jié)
在使用Nginx配置代理服務(wù)時,地址最后是否添加反斜杠(/)會對代理的URL處理產(chǎn)生影響,下面就來具體介紹一下,感興趣的可以了解一下2024-08-08
nginx運行報錯:unknown directive “stream“的解決方案
這篇文章主要給大家介紹了nginx 運行報錯:unknown directive "stream"的原因,主要是因為沒有安裝stream模塊導(dǎo)致的,我們只需要編譯安裝一下stream模塊即可解決這個問題,文中有詳細(xì)的解決方案,需要的朋友可以參考下2023-09-09
Nginx防止直接用IP訪問Web服務(wù)器的設(shè)置方法
看了很多Nginx的配置,好像都忽略了ip直接訪問Web的問題,這樣理論上不利于SEO優(yōu)化,所以我們希望可以避免直接用IP訪問網(wǎng)站,而是域名訪問,具體怎么做呢,看下面2012-09-09
在linux中安裝nginx及在樹莓派ubuntu中安裝nginx的方法
在進行軟件安裝時,若遇到報錯,常見的解決方法之一是檢查是否缺失依賴,并嘗試安裝所需的依賴包,記得去掉尖括號,對于nginx服務(wù)器,配置文件的位置和管理是關(guān)鍵操作之一,通過命令cd進入后,使用sudo nano index.nginx-debian.html命令打開并編輯默認(rèn)的html文件2024-11-11
nginx和lvs各自的優(yōu)劣以及適合的使用環(huán)境
這篇文章主要介紹了nginx和lvs各自的優(yōu)劣以及適合的使用環(huán)境,幫助大家選擇符合需求的服務(wù)器,感興趣的朋友可以了解下2020-10-10

