简体   繁体   中英

Compressing assets with NGINX in reverse proxy mode

I'm using NGINX as a reverse proxy in front of a Node.js app. The basic proxy works perfectly fine and I'm able to compress assets on the Node server with compression middleware.

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:

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. I've tried declaring these in the location context with different options but none of them seems to do the trick.

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. 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.

2- I'm testing all the responses with "Accept-Encoding: gzip" enabled.

Try to add the application/javascript content type:

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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