問題
我束手無策,所以決定發布一個查詢,在我注意到 lighthouse 抱怨我的一些客戶網站沒有啟用它之后,我試圖在 nginx docker 容器上設定 gzip 壓縮。所有違規站點在其配置中都有 gzip 壓縮設定,并添加了正確的設定和型別,但無論我如何更改它都是未壓縮的。我已經通過瀏覽器和使用 CURL 請求對此進行了測驗,沒有任何變化。
我在這里和其他地方查看了許多票,盡管有很多配置擺弄,但沒有一個解決方案有效。
我試過的
出于此票證和測驗的目的,我創建了一個完全“原始”的 docker 專案,其中default.conf包含一個包含站點配置的public/檔案和一個包含一個index.html和test-css.css檔案的檔案夾。
你可以在這里找到這個示例專案:
現在如果看一下test-css.css檔案,我們可以看到它沒有被壓縮:

Testing via console
So, after doing some research there appears to be instances were Chrome or various windows antiviruses and decompress things before reaching the browser. So I go and test from a linux VM using curl to attempt to fetch the file with gzip and see what it says.
master@apex:~$ curl -H "Accept-Encoding: gzip" -I http://www.apex.local/test-css.css
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Tue, 11 Jan 2022 15:38:00 GMT
Content-Type: text/css
Content-Length: 53790
Connection: keep-alive
Last-Modified: Tue, 11 Jan 2022 14:55:59 GMT
ETag: "61dd9a7f-d21e"
Accept-Ranges: bytes
So still no compression! So it doesn't appear to be the browser, additionally I used a site performance report website to test it and encountered the same issue.
From further research the issue can be if nginx doesn't have the gzip module installed so after digging I run this command from within the container which tests to see if it was compiled with gzip:
root@127b59858b48:/app# 2>&1 nginx -V | tr -- - '\n' | grep _module | grep gzip
http_gzip_static_module
So it has the module! So what the hey?
Additional information
我嘗試了多種配置設定組合,還嘗試了 nginx docker 映像的不同作業系統設定(我的專案在 alpine 上運行,因此認為這可能是一個因素),但結果是一致的。
客戶端 nginx 配置示例
從我的客戶網站復制/粘貼的當前配置示例:
gzip on;
gzip_min_length 10240;
gzip_comp_level 1;
gzip_vary on;
gzip_disable msie6;
gzip_proxied expired no-cache no-store private auth;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss xml
application/atom xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg xml;
參考票
我查看了很多票,但這些是最近在我的瀏覽器中打開的三張:
- https://serverfault.com/questions/915928/gzip-not-working-on-nginx/923396
- nginx gzip壓縮似乎不起作用
- nginx gzip壓縮不起作用
uj5u.com熱心網友回復:
這一切都適用于我的 Debian 11 主機
$:/so-issue-nginx-docker-example# curl -Iv -H "Accept-Encoding: gzip" 127.1:49153/test-css.css
* Trying 127.0.0.1:49153...
* Connected to 127.1 (127.0.0.1) port 49153 (#0)
> HEAD /test-css.css HTTP/1.1
> Host: 127.1:49153
> User-Agent: curl/7.74.0
> Accept: */*
> Accept-Encoding: gzip
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx/1.21.5
Server: nginx/1.21.5
< Date: Wed, 12 Jan 2022 09:12:35 GMT
Date: Wed, 12 Jan 2022 09:12:35 GMT
< Content-Type: text/css
Content-Type: text/css
< Last-Modified: Wed, 12 Jan 2022 09:04:47 GMT
Last-Modified: Wed, 12 Jan 2022 09:04:47 GMT
< Connection: keep-alive
Connection: keep-alive
< ETag: W/"61de99af-d21e"
ETag: W/"61de99af-d21e"
< Content-Encoding: gzip
Content-Encoding: gzip
我在您的瀏覽器和 curl 中注意到 NGINX 版本是 1.18.0。您共享的 Dockerfile 使用 NGINX 版本 1.21。鑒于此,您確定將 curl 發送到正確的實體,還是在流量到達“Dockerized”NGINX 之前有任何其他 NGINX 服務器代理流量?
你能再檢查一下嗎?我將使用 1.18 測驗設定以確保它不是該版本。
更新:它與1.18.0以及
HTTP/1.1 200 OK
< Server: nginx/1.18.0
Server: nginx/1.18.0
< Date: Wed, 12 Jan 2022 09:52:00 GMT
Date: Wed, 12 Jan 2022 09:52:00 GMT
< Content-Type: text/css
Content-Type: text/css
< Last-Modified: Wed, 12 Jan 2022 09:04:47 GMT
Last-Modified: Wed, 12 Jan 2022 09:04:47 GMT
< Connection: keep-alive
Connection: keep-alive
< ETag: W/"61de99af-d21e"
ETag: W/"61de99af-d21e"
< Content-Encoding: gzip
Content-Encoding: gzip
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/409323.html
標籤:
上一篇:NGINX404未找到但檔案存在
