Nginx服务器内容gzip压缩不起作用

5
这是我的nginx.conf文件中的一部分,但我不确定为什么在使用gzip压缩检查器或HTTP头时,内容没有被压缩。

https://pasify.com

user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  0;
    #keepalive_requests 5;
    #keepalive_timeout  65;
    send_timeout 10m;

    # output compression saves bandwidth
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

    gzip_buffers 16 8k;

    # Disable gzip for certain browsers.
    gzip_disable MSIE [1-6].(?!.*SV1);

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

    ## Detect when HTTPS is used
    map $scheme $fastcgi_https {
      default off;
      https on;
    }

    client_max_body_size 20M;


}

我能知道问题是什么吗?


如果您指的是网址,则是测试服务器是否压缩的地方。请在点击按钮之前告诉我们您的原因。 "关闭" - 1myb
请注意,gzip会禁用“sendfile”!! - Thomas Decaux
3个回答

8

通过

gzip_disable MSIE [1-6].(?!.*SV1);

你已经为几乎所有包含数字的浏览器禁用了gzip,因为有两个单独的正则表达式:"MSIE"和"[1-6].(?!.*SV1)"。请加上引号或者使用以下更好的方法:

gzip_disable msie6;

详细信息请参见文档


1
先生。我按照描述进行了更改,但仍然无法工作。您有什么可能的错误想法吗? - 1myb
我现在看到相关链接已经正确提供了gzip压缩的内容。如果你认为它仍然没有工作-请提供测试的详细信息。 - Maxim Dounin
是的..现在它可以工作了 =D 因为我猜工具太旧了,我发现它被其他工具一起压缩了... - 1myb

2
我唯一的评论是,http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_types中指出gzip_types除了text/html之外还可以压缩其他类型。因此,在gzip_types中指定text/html是不必要的。如果无论如何指定它都有问题,那么我认为这是一个错误,请尝试删除它以确保正确性。
如果这不是问题的根源,您能否向我们展示您的...
server {...}  

块是什么样子?

还要检查一下 /etc/nginx/conf.d/*.conf 中是否有设置 "gzip off" 的内容?


已检查,这是唯一的文件或位于etc/nginx/nginx.conf中,如上所述,含有关键字gzip。 - 1myb

0

Nginx的默认配置(至少v1.4.6)中,gzip_types行被注释掉了。必须将其取消注释,以便为列出的资源类型提供压缩服务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接