nginx gzip compression not working
Asked Answered
L

2

8

I have no idea where to place my gzip compression lines within my http block, shown here.

http {
    default_type application/octet-stream;
    include /etc/nginx/mime.types;

    log_format  main    '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    keepalive_timeout 65;

    server {

        listen 8080;

        root /usr/share/nginx;

        location / {
            root /usr/share/nginx/html;

            try_files $uri /index.html;

            autoindex off;
        }

        location ~ ^/(images|fonts|videos)/ {
            root /usr/share/nginx/assets;

            autoindex                off;
            expires                  7d;
            proxy_redirect           off;
            proxy_max_temp_file_size 0;

        }

        location ~ \.(mp3|mp4) {

        }
    }

    include /etc/nginx/conf.d/*.conf;
}

The lines I want to use for gzip compression are here, and I don't know whether to put these in the server block, before the server block, or in the location block:

# Compression
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_static on;

I have gzip_static set to "on" because I'm using gulp-gzip to compress various css and js files.

Ludicrous answered 29/2, 2016 at 19:21 Comment(5)
how did you determine that gzip is not functioning ? gzip is enabled bu default in nginx default installations, you can find it under nginx.conf file, however, you can add specific customizations for your appropriate case in the zone you see it fitLydon
By clicking the network tab I can see whether the content was gzipped by looking for a Content-Encoding field. For some reason my js / css files that I'd like to gzip don't display that field.Ludicrous
Here are the response headers for my build.min.js. Note how there is no Content-Encoding: gzip fieldLudicrous
it might be has something to do with the gzip_types you defined not matching what nginx can see, try to remove it and see if that is the case, nginx shall use gzip by defaultLydon
i just checked a configuration for one of my web apps and this was the conf that made it work, I hope it help you [gzip_types text/plain text/html text/css application/json application/javascript text/xml application/xml text/javascript;]Lydon
D
24

Edit your config file like this and it should work:

gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;

Note the added types, because sometimes those types can be detected in different ways by different systems.

Diseur answered 29/2, 2016 at 23:24 Comment(0)
I
0

I have my gzip config inside the server bracket. I used the config from @peixotorms for a more extended compression. The result in your case would be:

http {
    default_type application/octet-stream;
    include /etc/nginx/mime.types;

    log_format  main    '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    keepalive_timeout 65;

    server {

        # !ADDED GZIP CONFIG!
        gzip on;
        gzip_comp_level 6;
        gzip_vary on;
        gzip_types 
            text/plain 
            text/css 
            application/json 
            application/x-javascript 
            application/javascript 
            text/xml
            application/xml 
            application/rss+xml 
            text/javascript image/svg+xml 
            application/vnd.ms-fontobject 
            application/x-font-ttf 
            font/opentype;            

        listen 8080;

        root /usr/share/nginx;

        location / {
            root /usr/share/nginx/html;

            try_files $uri /index.html;

            autoindex off;
        }

        location ~ ^/(images|fonts|videos)/ {
            root /usr/share/nginx/assets;

            autoindex                off;
            expires                  7d;
            proxy_redirect           off;
            proxy_max_temp_file_size 0;

        }

        location ~ \.(mp3|mp4) {

        }
    }

    include /etc/nginx/conf.d/*.conf;
}

This

Infinity answered 19/8 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.