網站首頁 編程語言 正文
Nginx支持靜態和動態兩種包體gzip壓縮方式,分別對應模塊ngx_http_gzip_static,ngx_http_gzip。
我們知道gzip是CPU密集型的應用,實時動態壓縮比較消耗CPU資源。另外,如果使用gzip,則sendfile零拷貝技術無法使用。為進一步提高Nginx的性能,我們可以使用靜態gzip壓縮,提前將需要壓縮的文件壓縮好,當客服請求到達時,直接發送壓縮好的.gz文件,如此就減輕了服務器CPU的壓力,提高了性能。缺省ngx_http_gzip_static模塊并未啟用,需要重新編譯。
#注:根據需要自行添加其它參數
./configure --with-http_gzip_static_module
準備.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文件的修改時間。文件修改時間對應Last-Modified響應字段,HTTP緩存中使用很廣泛,同步二者時間,目的是保持緩存過期判斷的一致性。
touch index.html.gz -r index.html
添加配置文件:
gzip_static on;
gzip_static優先級高于gzip,$gzip_ratio對于gzip_static不生效,如果gzip_static失效,如缺少.gz,則gzip會生效。
gzip_static生效時,和gzip不同,Content-Encoding和Cotent-Length可以同時存在,因為響應在發送前已經明確其大小。
實際執行的效果:
[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參數
gzip_static always;
always的語義是不考慮客戶端是否支持gzip解壓【注:依據是客戶端發送的Accept-Encoding】,Nginx都將發送.gz文件,而on則是當客戶端不支持gzip解壓時,則發送原始文件。
下面是gzip_static on,curl啟用壓縮和不啟用壓縮的對比,可以看到僅當curl啟用壓縮才發送.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
下面是設置為gzip_static always,curl啟用壓縮和不啟用壓縮的對比,可以發現無論curl是否啟用壓縮,都將發送.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 #客戶端沒啟用壓縮,返回的內容仍然是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>
#因為沒有啟用壓縮,所以.gz文件無法解壓,將被當做二進制文件輸出,有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的發送,仿真是否需要響應的包體壓縮,看下圖:
總之,gzip_static是對gzip的補充,通過簡單的設置,就能使Nginx提供更好的性能。
參考:gzip_static
相關文章:一文讀懂nginx gzip
總結
原文鏈接:https://blog.csdn.net/weixin_45462681/article/details/123086969
相關推薦
- 2022-08-22 pytorch深度神經網絡入門準備自己的圖片數據_python
- 2022-06-07 Pytorch上下采樣函數之F.interpolate數組采樣操作詳解_python
- 2022-06-21 解決Git?Bash中文亂碼的問題_其它綜合
- 2023-01-07 Python個人博客程序開發實例信息顯示_python
- 2022-06-06 Postgresql split_part()函數,根據符號切割字符串
- 2022-11-14 Python語言中Tuple的由來分析_python
- 2022-07-07 python中list*n生成多維數組與for循環生成多維數組的區別說明_python
- 2022-07-28 Python自動化實戰之接口請求的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支