Thêm EC2 Origin

Thêm EC2 Origin

Trong bước này, bạn thêm EC2 instance làm origin thứ hai và định tuyến các request /api* tới origin này mà không lưu cache.

  1. Trong CloudFront console, mở distribution của bạn, chọn tab Origins, sau đó nhấn Create origin.

Create CloudFront Distribution

  1. Tại Origin domain, nhập public DNS name của EC2 instance. Tại Protocol, chọn HTTP only.

Create CloudFront Distribution

  1. Giữ các giá trị mặc định khác và mở rộng Additional settings.

Create CloudFront Distribution

  1. Đặt Keep-alive timeout là 60, sau đó nhấn Create origin.

Người dùng vẫn kết nối tới CloudFront qua HTTPS. Dùng HTTP giữa CloudFront và origin giúp tránh chi phí xử lý TLS trên kết nối này.

Create CloudFront Distribution

  1. Danh sách Origins hiện có thêm EC2 origin.

Create CloudFront Distribution

  1. Chọn tab Behaviors và nhấn Create behavior.

Create CloudFront Distribution

  1. Cấu hình behavior:
    • Path pattern: /api*
    • Origin and origin groups: chọn EC2 origin vừa tạo
    • Viewer protocol policy: Redirect HTTP to HTTPS
    • Allowed HTTP methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE

Create CloudFront Distribution

  1. Tại Cache key and origin requests, giữ Cache policy and origin request policy (recommended) và thiết lập:
    • Cache policy: CachingDisabled. CloudFront không lưu cache response, phù hợp với nội dung API động.
    • Origin request policy: AllViewer. CloudFront chuyển tiếp toàn bộ header, cookie và query string từ request của người dùng tới origin.

Create CloudFront Distribution

  1. Để trống Function associations và nhấn Create behavior.

Create CloudFront Distribution

  1. Behavior /api* xuất hiện với precedence 0, nằm trên behavior Default (*). Chờ distribution triển khai xong.

Create CloudFront Distribution