Home » Categories » VPS » VPS Linux

Thủ thuật tối ưu LAMP server qua htaccess

Tận dụng browser caching

Mì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ư:

  • images: jpg, gif, png
  • favicon/ico
  • javascript
  • css

Thời gian lưu cache thì bạn có thể tùy chọn

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Đâ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 Server

GZIP 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 Attachments
There are no attachments for this article.
Comments Comments
There are no comments for this article. Be the first to post a comment.
Related Articles RSS Feed
Hướng dẫn cài DirectAdmin Nulled mới nhất 2020
Xem 5657 - Đăng ngày Thu, Jul 2, 2020
Thay đổi Timezone trên Ubuntu
Xem 872 - Đăng ngày Fri, Feb 26, 2016
Thay đổi Timezone trên CentOS theo đúng múi giờ Việt Nam
Xem 796 - Đăng ngày Thu, Aug 11, 2016
Cập nhật phiên bản PHP 5.3.X lên 5.4.X hoặc 5.5.X trên CENTOS
Xem 3784 - Đăng ngày Wed, Mar 2, 2016
Hướng dẫn Tạo DKIM tên miền trên Zimbra Mail Server
Xem 867 - Đăng ngày Fri, Apr 17, 2020
Hướng dẫn kích hoạt Apache mod_rewrite trên CentOS
Xem 2378 - Đăng ngày Fri, Feb 26, 2016
Cài đặt EPEL và REMI Repo trên CentOS 7/6/5
Xem 1204 - Đăng ngày Sat, Feb 27, 2016
Cài đặt và bảo mật phpMyAdmin trên CentOS
Xem 1492 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn cài đặt và bảo mật phpMyAdmin trên Ubuntu 12.04
Xem 1269 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn kết nối sFTP bằng FileZilla
Xem 2907 - Đăng ngày Sat, Feb 27, 2016