2016-09-29 250 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