Hướng dẫn bật nén gzip cho website WordPress, giúp giảm page-size, tăng tốc độ load web.
Bạn kiểm tra tốc độ website với các công cụ như Google PageSpeed Insights hay GTmetrix và nhận được thông báo yêu cầu kích hoạt tính năng nén dữ liệu? Nó sẽ có dạng đại loại như “Compressing resources with gzip or deflate can reduce the number of bytes sent over the network” (Nén tài nguyên bằng gzip hoặc giảm số lượng có thể làm giảm số byte được gửi qua mạng). Vậy thì nén gzip là gì? Tại sao bạn nên bật nén gzip cho website của mình? Làm thế nào để bật nén gzip cho website WordPress một cách đơn giản nhất?
Contents
Nén gzip là gì?
Nén gzip hay gzip compression là một phương pháp nén tập tin (làm cho chúng nhỏ hơn) để truyền đi trên internet nhanh hơn. Nó được tiến hành trên web server trước khi truyền dữ liệu tới trình duyệt web của người dùng. Gzip có thể giúp bạn giảm tới hơn 80% dung lượng của các tập tin JS, CSS hay HTML.
Để kiểm tra xem website đã được bật nén gzip hay chưa, các bạn có thể sử dụng công cụ Check GZIP compression, Google PageSpeed Insights hay GTmetrix. Tất cả những gì bạn cần làm là nhập tên miền vào khung sau đó click vào biểu tượng kính lúp.
Một số link kiểm tra website đã bật gzip hay chưa
https://seomator.com/vi-VN/free-tools/compression-test
https://smallseotools.com/check-gzip-compression/
Bật nén gzip cho website WordPress
Trước hết, hãy kiểm tra xem web server bạn đang sử dụng thuộc loại nào (Apache, LiteSpeed hay NginX)? Nếu bạn không chắc chắn, hãy hỏi nhà cung cấp dịch vụ. Hướng dẫn này không chỉ áp dụng đối với website WordPress mà còn có thể áp dụng cho tất cả các mã nguồn website khác.
Đối với web server Apache hoặc LiteSpeed
Hai loại web server này đều hỗ trợ file .htaccess
. Trong cPanel, nếu bạn không tìm thấy file .htaccess
Chèn đoạn code sau đây vào đầu file .htaccess
để để bật nén gzip thông qua mod_deflate
:
<IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
Nếu web server của bạn không hỗ trợ mod_deflate
, hãy thay thế nó bằng mod_gzip
với code sau đây:
<ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_include mime ^text/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_include handler ^cgi-script$ </ifModule>
Đối với web server NginX
NginX không hỗ trợ file .htaccess
. Do đó, các bạn cần phải chèn đoạn code sau đây vào trong file nginx.conf
:
gzip on; gzip_comp_level 2; gzip_http_version 1.0; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_disable “MSIE [1-6].(?!.*SV1)”; gzip_vary on;