简体   繁体   English

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

[英]Compressing assets with NGINX in reverse proxy mode

I'm using NGINX as a reverse proxy in front of a Node.js app. 我正在将NGINX用作Node.js应用程序前面的反向代理。 The basic proxy works perfectly fine and I'm able to compress assets on the Node server with compression middleware. 基本代理可以正常工作,并且我可以使用compression中间件来压缩Node服务器上的资产。

To test if it's possible to delegate the compression task to NGINX, I've disabled the middleware and now I'm trying to gzip with NGINX with the following configuration: 为了测试是否有可能将压缩任务委托给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;
            }
    }
}

With this configuration, NGINX doesn't compress the assets. 使用此配置,NGINX不会压缩资产。 I've tried declaring these in the location context with different options but none of them seems to do the trick. 我尝试使用不同的选项在location上下文中声明它们,但似乎没有一个可以解决问题。

I couldn't find relevant resources on this so I'm questioning if it could be done this way at all. 我找不到与此相关的资源,所以我在质疑是否可以通过这种方式完成。

Important points: 要点:

1- Node and NGINX are on different containers so I'm not serving the static assets with NGINX. 1- Node和NGINX位于不同的容器上,因此我不使用NGINX提供静态资产。 I'm just proxying to the node server which is serving these files. 我只是代理到正在提供这些文件的节点服务器。 All I'm trying to achieve is offload the node server with getting NGINX to do the gzipping. 我要实现的所有工作都是通过让NGINX进行gzip压缩来卸载节点服务器。

2- I'm testing all the responses with "Accept-Encoding: gzip" enabled. 2-我正在启用“ Accept-Encoding:gzip”的情况下测试所有响应。

Try to add the application/javascript content type: 尝试添加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;

I took values from this conf H5BP : 我从这个conf H5BP中获取了值:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM