| Home » Categories » VPS » VPS Linux |
Thủ thuật tối ưu LAMP server qua htaccess |
|
Article Number: 16 | Rating: Unrated | Last Updated: Fri, Feb 26, 2016 at 11:33 PM
|
Tận dụng browser cachingMình sẽ hướng dẫn các bạn cách thêm Expires headers vào file .htaccess để tận dụng browser caching. Phương pháp này giúp giảm thiểu tối đa HTTP request đến server bằng cách sử dụng những file tĩnh như hình ảnh, css, javascript đã lưu trong cache của browser lúc bạn truy cập website lần đầu tiên. Cách thêm Expires header Trong ví dụ này mình sẽ thêm header cho một số loại file tĩnh như:
Thời gian lưu cache thì bạn có thể tùy chọn
Đây là đoạn code mình sử dụng, các bạn thêm vào cuối file .htaccess <IfModule mod_expires.c> # Enable expirations ExpiresActive On # Default directive ExpiresDefault "access plus 1 month" # My favicon ExpiresByType image/x-icon "access plus 1 year" # Images ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" # CSS ExpiresByType text/css "access 1 month" # Javascript ExpiresByType application/javascript "access plus 1 year" </IfModule> Kích hoạt GZIP Compression cho Apache ServerGZIP giúp tối ưu và tăng tốc website bằng cách nén nội dung trả về từ server mỗi khi có request gửi đến. Để thực hiện, các bạn chỉ cần thêm đoạn sau vào cuối file .htaccess. Apache 1.3 sử dụng mod_gzip trong khi Apache 2.x sử dụng mod_deflate. Để kiểm tra Apache Version các bạn sử dụng lệnh: httpd -v
Cấu hình mod_gzip cho Apache 1.3.x <IfModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </IfModule> Cấu hình mod_deflate cho Apache 2.x <IfModule mod_deflate.c> SetOutputFilter DEFLATE # Don't compress 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 #Dealing with proxy servers <IfModule mod_headers.c> Header append Vary User-Agent </IfModule> </IfModule> Để test lại xem đã thành công hay chưa các bạn sử dụng tool http://redbot.org, điền link site bạn vào và tìm xem có dòng Content-Encoding: gzip trong kết quả trả về là được. |
Attachments
There are no attachments for this article.
|
Comments ![]() There are no comments for this article. Be the first to post a comment. |
Cài đặt và bảo mật phpMyAdmin trên CentOS
Xem 1786 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn cài đặt LAMP (Linux, Apache, MySQL, PHP) trên CentOS 6
Xem 1320 - Đăng ngày Fri, Feb 26, 2016
Cài đặt FTP Server đơn giản trên CentOS với ProFTPD
Xem 3156 - Đăng ngày Sat, Feb 27, 2016
Hướng dẫn cài đặt LEMP (Linux, Nginx, MySQL, PHP) trên Ubuntu 12.04
Xem 1421 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn update Curl 7.29 lên bản mới nhất - Upgrade curl to latest on CentOS 7
Xem 1763 - Đăng ngày Wed, Apr 8, 2020
Hướng dẫn cài đặt chứng chỉ SSL (https) với Let’s Encrypt trên VPS/Server sử dụng DirectAdmin
Xem 3682 - Đăng ngày Tue, Apr 7, 2020
Hướng dẫn Tạo DKIM tên miền trên Zimbra Mail Server
Xem 1191 - Đăng ngày Fri, Apr 17, 2020
Thay đổi Timezone trên CentOS
Xem 807 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn cài DirectAdmin Nulled mới nhất 2020
Xem 6776 - Đăng ngày Thu, Jul 2, 2020
Thay đổi Timezone trên CentOS theo đúng múi giờ Việt Nam
Xem 1024 - Đăng ngày Thu, Aug 11, 2016
|



Add Comment