開啓Nginx的gzip壓縮功能

默認狀況下,Nginx的gzip壓縮是關閉的
同時,Nginx默認只對text/html進行壓縮
因此,開啓gzip的指令以下:

gzip on;
gzip_http_version 1.0;
gzip_disable "MSIE [1-6].";
gzip_types text/plain application/x-javascript text/css text/javascript;javascript

關於gzip_types,若是你想讓圖片也開啓gzip壓縮,那麼用如下這段吧:
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png; php

 

注意:
1. 其中的gzip_http_version的設置,它的默認值是1.1,就是說對HTTP/1.1協議的請求才會進行gzip壓縮
若是咱們使用了proxy_pass進行反向代理,那麼nginx和後端的upstream server之間是用HTTP/1.0協議通訊的
This module makes it possible to transfer requests to another server.
It is an HTTP/1.0 proxy without the ability for keep-alive requests yet. (As a result, backend connections are created and destroyed on every request.) Nginx talks HTTP/1.1 to the browser and HTTP/1.0 to the backend server. As such it handles keep-alive to the browser.
若是咱們使用nginx經過反向代理作Cache Server,並且前端的nginx沒有開啓gzip
同時,咱們後端的nginx上沒有設置gzip_http_version爲1.0,那麼Cache的url將不會進行gzip壓縮
 
2. gzip_disable的設置是禁用IE6的gzip壓縮,又是由於杯具的IE6
IE6的某些版本對gzip的壓縮支持很很差,會形成頁面的假死,今天產品的同窗就測試出了這個問題
後來調試後,發現是對img進行gzip後形成IE6的假死,把對img的gzip壓縮去掉後就正常了
爲了確保其它的IE6版本不出問題,因此就加上了gzip_disable的設置css

相關文章
相關標籤/搜索