Bạn đang lưu ảnh tại Vultr Object Storage và muốn tạo một hệ thống CDN ảnh riêng (dùng domain như cdn.domain.com) có:

✅ Tốc độ nhanh toàn cầu
✅ HTTPS đầy đủ
✅ Ẩn hoàn toàn URL gốc từ Vultr
✅ Không cần dùng dịch vụ CDN trả phí

Bạn sẽ kết hợp: Cloudflare CDN + Nginx Reverse Proxy + Vultr Object Storage.


📌 Mô hình tổng thể

Client (trình duyệt)
     ↓
cdn.domain.com (qua Cloudflare proxy + HTTPS)
     ↓
Cloudflare Edge Cache (toàn cầu)
     ↓
Nginx Reverse Proxy (server của bạn)
     ↓
Vultr Object Storage (nguồn ảnh gốc)

🧩 Yêu cầu chuẩn bị

  • Một server VPS có cài Nginx
  • Vultr Object Storage đã có bucket (ví dụ mybucket)
  • Một domain, ví dụ: cdn.domain.com
  • Cloudflare (miễn phí là đủ)

🔧 Bước 1: Trỏ DNS cdn.domain.com về VPS

  1. Vào Cloudflare → chọn domain chính (ví dụ: domain.com)
  2. Thêm record:
    • Type: A
    • Name: cdn
    • IP: IP VPS của bạn
    • Proxy: bật màu cam (Cloudflare proxy)

🖥️ Bước 2: Cấu hình Nginx reverse proxy

1. Tạo thư mục cache:

sudo mkdir -p /var/cache/nginx/vultr_cache

2. Tạo cấu hình Nginx: /etc/nginx/sites-available/cdn.domain.com

proxy_cache_path /var/cache/nginx/vultr_cache levels=1:2 keys_zone=cloudflare_cache:100m inactive=24h max_size=1g;

server {
    listen 80;
    server_name cdn.domain.com;

    location / {
        proxy_pass https://ewr1.vultrobjects.com/mybucket/;
        proxy_set_header Host ewr1.vultrobjects.com;

        proxy_cache cloudflare_cache;
        proxy_cache_key "$host$request_uri";
        proxy_cache_valid 200 301 302 1d;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;

        add_header X-Cache $upstream_cache_status;
        add_header Access-Control-Allow-Origin *;
    }
}

⚠️ Thay ewr1.vultrobjects.commybucket bằng endpoint & bucket thật từ Vultr.

3. Kích hoạt site:

sudo ln -s /etc/nginx/sites-available/cdn.domain.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

🔒 Bước 3: Cài đặt SSL miễn phí với Let’s Encrypt

Mặc dù Cloudflare proxy HTTPS, bạn vẫn cần cài SSL trên Nginx để dùng chế độ Full (strict) – bảo mật cao nhất.

1. Mở port 80/443:

sudo ufw allow 80
sudo ufw allow 443

2. Cài Certbot:

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

3. Cấp chứng chỉ SSL:

sudo certbot --nginx -d cdn.domain.com

✔️ Nếu thành công, Certbot sẽ tự thêm block listen 443 ssl và cấu hình SSL.


🔁 Bước 4: Cấu hình Cloudflare

Trong dashboard Cloudflare:

  1. SSL/TLS → Chọn “Full (strict)”
  2. Rules (hoặc Page Rules) → thêm:
cdn.domain.com/*
→ Cache Level: Cache Everything
→ Edge Cache TTL: 1 month
  1. Bật "Always Use HTTPS" nếu muốn redirect toàn bộ về HTTPS.

🧪 Bước 5: Kiểm tra hoạt động

Truy cập thử:

https://cdn.domain.com/path/to/image.jpg

Sau đó kiểm tra header:

curl -I https://cdn.domain.com/path/to/image.jpg

Bạn nên thấy:

  • X-Cache: HIT hoặc MISS (cache Nginx)
  • CF-Cache-Status: HIT (cache Cloudflare)

🛡️ Tuỳ chọn bảo mật thêm

  • Nếu muốn bảo mật tuyệt đối, có thể thiết lập ACL trong Vultr chỉ cho phép truy cập từ IP server của bạn.
  • Nếu ảnh nhạy cảm, có thể dùng pre-signed URL từ Vultr (nếu hỗ trợ).
  • Hoặc cài thêm lớp xác thực ở Nginx nếu cần giới hạn quyền truy cập.

✅ Tổng kết lợi ích

Tính năngĐã có
Domain CDN riêng (SSL)
Ẩn endpoint Vultr
Cache 2 lớp (Cloudflare + Nginx)
Chi phí rẻ, kiểm soát tốt
Mở rộng dễ dàng

🚀 Gợi ý nâng cao

Bạn có thể:

  • Tự động gia hạn SSL với cron job (certbot renew)
  • Kết hợp hệ thống tạo ảnh thumbnail tự động (/image.jpg?w=300)
  • Dùng X-Accel-Redirect để xử lý cache đặc biệt

📁 Demo cấu trúc

/etc/nginx/sites-available/cdn.domain.com
/var/cache/nginx/vultr_cache/

☑️ Kết thúc!

Bạn đã hoàn thành việc tạo hệ thống CDN ảnh riêng miễn phí kết hợp:

  • Cloudflare (CDN toàn cầu)
  • Nginx (proxy + cache)
  • Vultr Object Storage (lưu trữ ảnh gốc)