Nginx開啟Gzip壓縮提高頁面載入速度
阿新 • • 發佈:2018-12-12
# 開啟Nginx配置檔案 vim /usr/local/nginx/conf/nginx.conf # 找到如下,進行修改 gzip on; // 開啟Gzip gzip_min_length 1k; // 不壓縮臨界值,大於1K的才壓縮,一般不用改 gzip_buffers 4 16k; // buffer,不用修改 #gzip_http_version 1.0; // 反向代理末端通訊HTTP/1.0,預設是HTTP/1.1 gzip_comp_level 2; // 壓縮級別,1-10,數字越大壓縮越高,時間越長 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; // 壓縮檔案型別,注意JavaScript的兩種寫法 gzip_vary off; // 快取服務有關,開啟會在Header裡增加"Vary: Accept-Encoding" gzip_disable "MSIE [1-6]\."; // 設定不壓縮,IE1-6不壓縮 # 儲存退出 # 重新載入Nginx /usr/local/nginx/sbin/nginx -s reload # 測試頁面Gzip是否成功開啟 curl -I -H "Accept-Encoding: gzip, deflate" "網站域名路徑" # 頁面成功壓縮 HTTP/1.1 200 OK Server: nginx/1.0.15 Date: 26 Sep 2018 11:13:09 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/7.2.10 X-Pingback: 網站域名路徑/index.php Content-Encoding: gzip # 測試CSS Gzip是否成功開啟 curl -I -H "Accept-Encoding: gzip, deflate" "網站域名路徑/css/public.css" # CSS檔案成功壓縮 HTTP/1.1 200 OK Server: nginx/1.0.15 Date: 26 Sep 2018 11:21:25 GMT Content-Type: text/css Last-Modified: 24 Sep 2018 09:47:53 GMT Connection: keep-alive Expires: 27 Aug 2012 06:21:25 GMT Cache-Control: max-age=43200 Content-Encoding: gzip # 測試JS Gzip是否成功開啟 curl -I -H "Accept-Encoding: gzip, deflate" "網站域名路徑/js/jquery.js" # JS檔案成功壓縮 HTTP/1.1 200 OK Server: nginx/1.0.15 Date: 26 Sep 2018 11:21:38 GMT Content-Type: application/x-javascript Last-Modified: 24 Sep 2018 09:48:23 GMT Connection: keep-alive Expires: 26 Sep 2018 23:21:38 GMT Cache-Control: max-age=43200 Content-Encoding: gzip # 測試JS Gzip是否成功開啟 curl -I -H "Accept-Encoding: gzip, deflate" "網站域名路徑/images/logo.png" # 圖片成功壓縮 HTTP/1.1 200 OK Server: nginx/1.0.15 Date: 26 Sep 2018 11:22:45 GMT Content-Type: image/png Last-Modified: 24 Sep 2018 09:49:13 GMT Connection: keep-alive Expires: 25 Oct 2018 11:22:45 GMT Cache-Control: max-age=2592000 Content-Encoding: gzip # 測試小於1K檔案Gzip是否成功開啟 curl -I -H "Accept-Encoding: gzip, deflate" "網站域名路徑/css/cases.css" # 小於1K檔案不壓縮 HTTP/1.1 200 OK Server: nginx/1.0.15 Date: 26 Sep 2018 11:23:27 GMT Content-Type: text/css Content-Length: 180 Last-Modified: 24 Sep 2018 09:47:45 GMT Connection: keep-alive Expires: 26 Sep 2018 11:23:27 GMT Cache-Control: max-age=43200 Accept-Ranges: bytes