Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 231
cinnaroll45 Avatar asked Oct 02 '17 17:10

cinnaroll45


People also ask

Can Nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

How do I compress images in Nginx?

By default, NGINX compresses responses only with MIME type text/html . To compress responses with other MIME types, include the gzip_types directive and list the additional types. gzip_types text/plain application/xml; To specify the minimum length of the response to compress, use the gzip_min_length directive.

Is Nginx a proxy or reverse proxy?

NGINX Plus and NGINX are the best-in-class reverse proxy and load balancing solutions used by high-traffic websites such as Dropbox, Netflix, and Zynga. More than 350 million websites worldwide rely on NGINX Plus and NGINX Open Source to deliver their content quickly, reliably, and securely.


1 Answers

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:

like image 181
nbari Avatar answered Sep 30 '22 19:09

nbari