在反向代理模式下使用 NGINX 压缩资产

cin*_*l45 6 gzip reverse-proxy nginx node.js

我在 Node.js 应用程序前面使用 NGINX 作为反向代理。基本代理运行良好,我能够使用compression中间件压缩 Node 服务器上的资产。

为了测试是否可以将压缩任务委托给 NGINX,我禁用了中间件,现在我尝试使用以下配置使用 NGINX 进行 gzip:

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 300;
    server {
        listen 80;

        ## gzip config
        gzip on;
        gzip_min_length 1000;
        gzip_comp_level 5;
        gzip_proxied any;
        gzip_vary on;
        gzip_types text/plain
                   text/css
                   text/javascript
                   image/gif
                   image/png
                   image/jpeg
                   image/svg+xml
                   image/x-icon;

        location / {
            proxy_pass http://app:3000/;
            proxy_http_version 1.1;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_cache_bypass $http_upgrade;
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此配置,NGINX 不会压缩资产。我试过在location上下文中用不同的选项声明这些,但似乎没有一个能解决问题。

我找不到相关的资源,所以我怀疑是否可以这样做。

要点:

1- Node 和 NGINX 位于不同的容器上,所以我不使用 NGINX 提供静态资产。我只是代理到为这些文件提供服务的节点服务器。我想要实现的只是通过让 NGINX 执行 gzip 来卸载节点服务器。

2- 我正在测试所有启用了“接受编码:gzip”的响应。

nba*_*ari 8

尝试添加application/javascript内容类型:

gzip_types
    text/css
    text/javascript
    text/xml
    text/plain
    text/x-component
    application/javascript
    application/json
    application/xml
    application/rss+xml
    font/truetype
    font/opentype
    application/vnd.ms-fontobject
    image/svg+xml;
Run Code Online (Sandbox Code Playgroud)

我从这个conf H5BP 中获取了值: