nginx gzip 压缩不起作用

nginx gzip compression not working

我不知道在我的 http 块中将我的 gzip 压缩行放在哪里,如下所示。

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

我要用于gzip压缩的行在这里,我不知道是把它们放在服务器块中,在服务器块之前,还是在位置块中:

# 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;

我将 gzip_static 设置为 "on" 因为我正在使用 gulp-gzip 来压缩各种 css 和 js 文件。

像这样编辑您的配置文件,它应该可以工作:

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;

注意添加的类型,因为有时不同的系统会以不同的方式检测到这些类型。