網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Nginx支持靜態(tài)和動(dòng)態(tài)兩種包體gzip壓縮方式,分別對(duì)應(yīng)模塊ngx_http_gzip_static,ngx_http_gzip。
我們知道gzip是CPU密集型的應(yīng)用,實(shí)時(shí)動(dòng)態(tài)壓縮比較消耗CPU資源。另外,如果使用gzip,則sendfile零拷貝技術(shù)無法使用。為進(jìn)一步提高Nginx的性能,我們可以使用靜態(tài)gzip壓縮,提前將需要壓縮的文件壓縮好,當(dāng)客服請(qǐng)求到達(dá)時(shí),直接發(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文件的修改時(shí)間。文件修改時(shí)間對(duì)應(yīng)Last-Modified響應(yīng)字段,HTTP緩存中使用很廣泛,同步二者時(shí)間,目的是保持緩存過期判斷的一致性。
touch index.html.gz -r index.html
添加配置文件:
gzip_static on;
gzip_static優(yōu)先級(jí)高于gzip,$gzip_ratio對(duì)于gzip_static不生效,如果gzip_static失效,如缺少.gz,則gzip會(huì)生效。
gzip_static生效時(shí),和gzip不同,Content-Encoding和Cotent-Length可以同時(shí)存在,因?yàn)轫憫?yīng)在發(fā)送前已經(jīng)明確其大小。
實(shí)際執(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的語(yǔ)義是不考慮客戶端是否支持gzip解壓【注:依據(jù)是客戶端發(fā)送的Accept-Encoding】,Nginx都將發(fā)送.gz文件,而on則是當(dāng)客戶端不支持gzip解壓時(shí),則發(fā)送原始文件。
下面是gzip_static on,curl啟用壓縮和不啟用壓縮的對(duì)比,可以看到僅當(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啟用壓縮和不啟用壓縮的對(duì)比,可以發(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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#因?yàn)闆]有啟用壓縮,所以.gz文件無法解壓,將被當(dāng)做二進(jìn)制文件輸出,有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是對(duì)gzip的補(bǔ)充,通過簡(jiǎn)單的設(shè)置,就能使Nginx提供更好的性能。
參考:gzip_static
相關(guān)文章:一文讀懂nginx gzip
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_45462681/article/details/123086969
相關(guān)推薦
- 2022-09-23 windows10本地搭建FTP服務(wù)器圖文教程_FTP服務(wù)器
- 2022-09-23 win11下FTP服務(wù)器搭建圖文教程_FTP服務(wù)器
- 2022-08-02 Python?的矩陣傳播機(jī)制Broadcasting和矩陣運(yùn)算_python
- 2022-04-28 Python模塊pexpect安裝及使用流程_python
- 2022-08-04 Go語(yǔ)言學(xué)習(xí)之WaitGroup用法詳解_Golang
- 2022-11-06 python?基本結(jié)構(gòu)語(yǔ)句(函數(shù)和模塊)_python
- 2022-07-18 Android 防重復(fù)點(diǎn)擊(Kotlin 協(xié)程實(shí)現(xiàn) 和 Handler實(shí)現(xiàn))
- 2022-10-24 Django使用Redis進(jìn)行緩存詳細(xì)步驟_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支