Bên cạnh việc thiết lập [cache cho các file tĩnh](http://blog.nvb-online.com/101-apache-thiet-lap-de-browser-cache-cac-file-tinh-voi-mod-expires), thì việc nén các file trước khi gửi trả về client cũng giúp tăng thời gian load trang và giảm băng thông truyền tải của server. Dưới đây là thiết lập cho Apache để bật chức năng nén gzip. Môi trường thiết đặt: * Centos 6.8 * Apache 2.4 ### Load module mod_deflate Thêm dòng sau vào file config của Apache (thông thường là file `/etc/httpd/conf/httpd.conf`) ~~~ LoadModule deflate_module modules/mod_deflate.so ~~~ ### Thiết lập mod_deflate Thiết lập cho mod_deflate có thể đặt trong thiết lập chung của Apache, trong container `VirtualHost`, trong thẻ `` hoặc trong file `.htaccess`. Ở ví dụ dưới đây sẽ đặt thiết lập cho mod_deflate trong thiết lập chung của Apache để áp dụng cho toàn server. Thêm nội dung sau vào file config của Apache: ~~~ SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # You can't compress what is already compressed SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary # Make proxies work as they should. Header append Vary User-Agent ~~~ ### Khởi động lại Apache ~~~ $ sudo service httpd restart ~~~ Sau khi khởi động lại Apache, có thể kiểm tra website đã được bật chức năng nén gzip chưa bằng tool ở địa chỉ sau: https://varvy.com/tools/gzip/