2016-09-29 78 views
1

我想在我的nginx服务器上启用gzip压缩。该nginx.conf文件是在这里:Nginx启用gzip

http { 
    # Enable Gzip 
    server { 

    location ~* \.(?:ico|woff|css|js|gif|jpe?g|png)$ { 
     expires 30d; 
     add_header Pragma public; 
     add_header Cache-Control "public"; 
    } 

    location /api { 
     try_files $uri $uri/ /api/index.php; 
    } 

    location/{ ##merge 
     gzip on; 
     gzip_http_version 1.0; 
     gzip_comp_level 2; 
     gzip_min_length 1100; 
     gzip_buffers  4 8k; 
     gzip_proxied any; 
     gzip_types 
      # text/html is always compressed by HttpGzipModule 
      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; 

     gzip_static on; 

     gzip_proxied  expired no-cache no-store private auth; 
     gzip_disable  "MSIE [1-6]\."; 
     gzip_vary   on; 

     try_files $uri $uri/ /index.php?q=$uri&$args; 
    } 

    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } 
    location ~ "^/ngx_pagespeed_static/" { } 
    location ~ "^/ngx_pagespeed_beacon" { } 

    } 
} 

不幸的是,gzip压缩不工作,谷歌的PageSpeed和Gtmetrix无法检测到这一点。

我可以在哪里放置gzip conf?

http{}server{}location{}标记?

我已经尝试过在http,并在location标签太

回答

8

你可以把gzip的配置在任何地方,但如果你想将它应用到所有网站/文件最好是把它的HTTP部分 - 这将成为所有服务器和位置块的默认设置。我也将“缩短” /更改配置为以下内容:

http { 
    gzip on; 
    gzip_min_length 500; 
    gzip_proxied  any; 
    gzip_comp_level 4; 
    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; 
    gzip_vary on; 
    gzip_disable  "msie6"; 

    ... here come your server blocks/rest of your config 
} 

我使用的配置,它为我工作正常 - 测试它之前,你还可以测试它在浏览器中第一个(例如使用Firebug)与外部服务。

使用gzip_static只有在您为Nginx生成gzip文件(如文件名+ .gz)时才有意义,所以这与启用gzip无关,应该只是第二步。

+0

谢谢@quito它的作品! – wpdaniel