欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章讀懂nginx的gzip_static模塊

 更新時間:2022年05月12日 15:15:06   作者:乘風(fēng)破浪2021  
gzip是針對于請求實(shí)時進(jìn)行壓縮,cpu開銷大,gzip_static?完全可以在編譯后使用壓縮工具搞出來,下面這篇文章主要給大家介紹了如何通過一篇文章讀懂nginx的gzip_static模塊,需要的朋友可以參考下

Nginx支持靜態(tài)和動態(tài)兩種包體gzip壓縮方式,分別對應(yīng)模塊ngx_http_gzip_static,ngx_http_gzip。

我們知道gzip是CPU密集型的應(yīng)用,實(shí)時動態(tài)壓縮比較消耗CPU資源。另外,如果使用gzip,則sendfile零拷貝技術(shù)無法使用。為進(jìn)一步提高Nginx的性能,我們可以使用靜態(tài)gzip壓縮,提前將需要壓縮的文件壓縮好,當(dāng)客服請求到達(dá)時,直接發(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à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的語義是不考慮客戶端是否支持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>

#因?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是對gzip的補(bǔ)充,通過簡單的設(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)文章

最新評論