開啟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;
關于gzip_types,如果你想讓圖片也開啟gzip壓縮,那么用以下這段吧:
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
注意:
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的設置