Static website hosting trên Amazon S3 và trang dự phòng với Route 53 failover
Một website chỉ gồm HTML, CSS và ảnh không cần tầng ứng dụng như EC2 instance; các object được phục vụ trực tiếp từ bucket S3.
Trang khuyến mãi theo mùa của MFTECH chỉ gồm HTML, CSS, ảnh và client-side script, nên có thể được phục vụ trực tiếp từ bucket mftech-promo. Khi bật static website hosting, bucket có thêm một website endpoint theo Region (http://<bucket>.s3-website-<region>.amazonaws.com hoặc …s3-website.<region>… tuỳ Region), tách biệt với REST API endpoint mà ứng dụng dùng để gửi request API. Hai endpoint xử lý cùng một request theo cách rất khác nhau. Gửi từng request qua mỗi endpoint để so sánh:
Chọn endpoint và request, sau đó nhấn Gửi request. Gửi cùng request qua endpoint còn lại để so sánh.
Website endpoint được tối ưu cho trình duyệt: trả về index document tại thư mục gốc, trả về tài liệu HTML (custom error document với lỗi 4XX) khi có lỗi, hỗ trợ redirect, nhưng chỉ phục vụ nội dung đọc công khai, chỉ nhận GET và HEAD, và không hỗ trợ HTTPS. AWS khuyến nghị dùng AWS Amplify Hosting hoặc đặt CloudFront phía trước khi cần HTTPS. Website tĩnh cũng không thay thế được tầng ứng dụng: S3 không chạy server-side script, nên trang có nội dung động thay đổi liên tục (giỏ hàng, giá theo thời gian thực) vẫn cần EC2 trong Auto Scaling group hoặc dịch vụ tương đương để mở rộng theo lưu lượng.
Website tĩnh trên S3 thường được dùng làm trang dự phòng: khi website chính phía sau ALB không phản hồi, khách hàng thấy trang thông báo bảo trì thay vì lỗi kết nối. Thực hiện lần lượt từng bước:
- Website tĩnh (HTML, CSS, ảnh, client-side script): S3 static website hosting.
- Website endpoint: chỉ GET/HEAD, chỉ nội dung công khai, trả về index document và trang lỗi HTML, không có HTTPS.
- Tải lên qua CloudFront: origin là REST endpoint của bucket với OAC, không phải website endpoint.
- Trang bảo trì khi website chính gặp sự cố: Route 53 failover routing (active-passive), secondary là alias tới website endpoint.
- Nội dung động: cần tầng ứng dụng, không đặt trên S3.
Static website hosting on Amazon S3 and a failover page with Route 53
A website made only of HTML, CSS and images needs no application tier such as EC2 instances; the objects are served directly from an S3 bucket.
MFTECH’s seasonal promotion site consists only of HTML, CSS, images and client-side scripts, so it can be served directly from the mftech-promo bucket. When you enable static website hosting, the bucket gets a Region-specific website endpoint (http://<bucket>.s3-website-<region>.amazonaws.com or …s3-website.<region>…, depending on the Region), separate from the REST API endpoint that applications send API requests to. The two endpoints handle the same request very differently. Send each request through each endpoint to compare:
Chọn endpoint và request, sau đó nhấn Gửi request. Gửi cùng request qua endpoint còn lại để so sánh.
The website endpoint is optimized for browsers: it returns the index document at the root, returns an HTML document (the custom error document for 4XX errors) on errors and supports redirects, but it serves only publicly readable content, accepts only GET and HEAD, and does not support HTTPS. AWS recommends AWS Amplify Hosting, or putting CloudFront in front, when you need HTTPS. A static website does not replace an application tier either: S3 does not run server-side scripts, so pages with dynamic content that changes constantly (a cart, real-time prices) still need EC2 in an Auto Scaling group or an equivalent service to scale with traffic.
A static website on S3 is often used as a failover page: when the main website behind the ALB stops responding, customers see a maintenance notice instead of a connection error. Perform each step in turn:
- Static website (HTML, CSS, images, client-side scripts): S3 static website hosting.
- Website endpoint: GET/HEAD only, public content only, returns the index document and HTML error pages, no HTTPS.
- Uploads through CloudFront: the origin is the bucket’s REST endpoint with OAC, not the website endpoint.
- A maintenance page when the main website fails: Route 53 failover routing (active-passive) with the secondary as an alias to the website endpoint.
- Dynamic content: needs an application tier, not S3.