css/js開啟nginx的gzip壓縮

來源:互聯網
上載者:User

最近兩天在做Ghost的主題Adam。我的部落格是跑在境外伺服器上的,所以為了訪問速度快點,啟用gzip壓縮是必不可少的。

vim /etc/nginx/nginx.conf

找到類似如下代碼:

gzip on
gzip_types text/plain

改為如下即可:

gzip on;
gzip_buffers 4 16k;
#gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript;
gzip_comp_level 6;
gzip_vary on;
gzip_disable "MSIE";
gzip_proxied any;

測試的話,可以用curl測試,例如:

curl -I -H "Accept-Encoding: gzip, deflate" "/assets/js/script.js"

返回

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Tue, 01 Jul 2014 17:25:14 GMT
Content-Type: application/javascript
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: Express
ETag: "24792-1404232049000"
Cache-Control: public, max-age=3600
Last-Modified: Tue, 01 Jul 2014 16:27:29 GMT
Content-Encoding: gzip
可看到Content-Encoding: gzip,證明是開啟了的。
如果沒有開啟成功的話,可以檢查下/etc/nginx/mime.types 裡面的類型是否對應nginx.conf中的gzip_types。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.