【问题标题】:Nginx gzip compression not working despite following official Nginx documentation / online research尽管遵循官方 Nginx 文档/在线研究,Nginx gzip 压缩仍无法正常工作
【发布时间】:2022-01-12 20:55:34
【问题描述】:

问题

我束手无策,所以决定发布一个查询,在我注意到 lighthouse 抱怨我的一些客户网站没有启用它之后,我试图在 nginx docker 容器上设置 gzip 压缩。所有违规站点在其配置中都有 gzip 压缩设置,并添加了正确的设置和类型,但无论我如何更改它都是未压缩的。我已经通过浏览器和使用 CURL 请求对此进行了测试,没有任何变化。

我在这里和其他地方查看了很多票,尽管有很多配置摆弄,但没有一个解决方案有效。

我尝试过的

出于此票证和测试的目的,我创建了一个完全“原始”的 docker 项目,其中包含一个包含站点配置的 default.conf 文件和一个包含 index.htmltest-css.css 文件的 public/ 文件夹。

您可以在此处找到此示例项目:https://github.com/MajorFailz/so-issue-nginx-docker-example

config文件只是服务于文件夹,这里包含了nginx自己的文章推荐的gzip设置:https://docs.nginx.com/nginx/admin-guide/web-server/compression/

Dockerfile

FROM nginx:1.21

RUN mkdir /app

WORKDIR /app

COPY ./public /app/public

COPY ./conf/default.conf /etc/nginx/conf.d/default.conf

default.conf

server {
    gzip on;
    gzip_types      text/plain application/xml text/css;
    gzip_proxied    no-cache no-store private expired auth;
    gzip_min_length 1000;

    location / {
        root /app/public;
    }
}

用浏览器测试

当这个项目启动并在 chrome 中查看时,打开开发选项卡并查看 index.html 显示它正在使用 gzip 压缩:

现在如果查看test-css.css 文件,我们可以看到它没有被压缩:

通过控制台测试

因此,在进行了一些研究之后,似乎有一些实例是 Chrome 或各种 Windows 防病毒软件,并在到达浏览器之前解压缩了一些东西。因此,我使用 curl 从 linux VM 进行测试,尝试使用 gzip 获取文件并查看它的内容。

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

所以仍然没有压缩!所以它似乎不是浏览器,另外我使用了一个站点性能报告网站来测试它并遇到了同样的问题。

从进一步的研究来看,问题可能是 nginx 没有安装 gzip 模块,所以在挖掘之后我从容器中运行这个命令来测试它是否是用 gzip 编译的:

root@127b59858b48:/app# 2>&1 nginx -V | tr -- - '\n' | grep _module | grep gzip
http_gzip_static_module

所以它有模块!那么,嘿嘿?

其他信息

我尝试了多种配置设置组合,还为 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;

门票参考

我查看了很多票,但这些是最近在我的浏览器中打开的三张:

【问题讨论】:

    标签: javascript css docker nginx gzip


    【解决方案1】:

    这一切都适用于我的 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
    
    

    【讨论】:

    • 你的传说,我的本地开发环境和我的客户端站点都通过托管服务器上的单独 nginx 实例代理 docker nginx 连接,因此它被压缩到 docker 实例上,然后在主机上解压缩,然后发送解压后退出。有时只需要另一双眼睛,谢谢你花时间研究这个,你让我免于完全发疯。对于像我这样的大笨蛋的其他人来说,值得将其添加到您的答案中;)
    猜你喜欢
    • 2020-09-30
    • 2016-09-15
    • 1970-01-01
    • 2018-10-11
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多