【问题标题】:Compressing assets with NGINX in reverse proxy mode在反向代理模式下使用 NGINX 压缩资产
【发布时间】:2018-03-13 20:23:35
【问题描述】:

我在 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;
            }
    }
}

使用此配置,NGINX 不会压缩资源。我尝试使用不同的选项在 location 上下文中声明这些,但它们似乎都不起作用。

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

要点:

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

2- 我正在测试启用了“Accept-Encoding: gzip”的所有响应。

【问题讨论】:

    标签: node.js nginx gzip reverse-proxy


    【解决方案1】:

    尝试添加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;
    

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

    【讨论】:

    • 是的,这就是我应该使用的正确 MIME 类型。
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 2019-09-23
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多