Enable Gzip Compression Apache

Gzip Compression được các browser (trình duyệt) hiện nay hỗ trợ để nén dữ liệu truyền tải giữa Server & Browser nhằm tăng tốc độ tải và tiết kiệm băng thông. xTraffic.pep.vn sẽ giới thiệu đến các bạn cách kích hoạt tính năng nén Gzip trên Apache

Enable Gzip Compression Apache - Apache gzip htaccess httpd - Phát triển website

Nếu bạn chưa biết cách cài đặt Apache (Web Server) thì có thể tham khảo thêm cách cài đặt Apache + PHP + MySQL (LAMP) tại đây

Có 2 cách bật tính năng Gzip Compression trên Apache là cấu hình tại file “httpd.conf” hoặc file “.htaccess” . xTraffic.pep.vn khuyến cáo bạn sử dụng file httpd.conf vì nó nhanh hơn và áp dụng cho tất cả các website trên Server. Tuy nhiên, nếu bạn đang sử dụng Shared Hosting thì bạn sẽ không có quyền chỉnh sửa file httpd.conf , lúc này bạn bắt buộc phải sử dụng file “.htaccess” để bật Gzip.

Bật Gzip Compression trên Apache bằng httpd.conf

Bạn mở file “/etc/httpd/conf/httpd.conf” và thêm vào các dòng sau vào cuối file :

<ifModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/* text/html text/xml text/css text/plain text/x-component text/x-js text/richtext text/xsd text/xsl
    AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml image/x-icon
    AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
    AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
    AddOutputFilterByType DEFLATE font/truetype font/opentype
    Header append Vary User-Agent env=!dont-vary
    AddOutputFilter DEFLATE js css htm html xml text
</ifModule>

Sau đó lưu lại file và khởi động lại Apache bằng lệnh sau :

service httpd restart

Bật Gzip Compression trên Apache bằng .htaccess

Bạn nên tạo 1 file “.htaccess” tại thư mục gốc của website (trên Shared Hosting thông thường là thư mục public_html) và thêm vào các nội dung sau vào file .htaccess

<ifModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/* text/html text/xml text/css text/plain text/x-component text/x-js text/richtext text/xsd text/xsl
    AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml image/x-icon
    AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
    AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
    AddOutputFilterByType DEFLATE font/truetype font/opentype
    Header append Vary User-Agent env=!dont-vary
    AddOutputFilter DEFLATE js css htm html xml text
</ifModule>

Leave a Comment