Hướng dẫn thực chiến: Cấu hình Domain với Cloudflare Proxy, Origin SSL Full (Strict), Nginx và Deploy bằng rsync
Triển khai thực tế domain toannh8.com và subdomain manga.toannh8.com lên Linux VPS phía sau Cloudflare Proxy, kèm 2 bài học thực chiến về lỗi HTTP 525 và bẫy mất Security Headers trong Nginx.

Hướng dẫn thực chiến: Cấu hình Domain với Cloudflare Proxy, Origin SSL Full (Strict) và Nginx trên VPS
Khi mua một tên miền mới và muốn đưa website lên VPS (như Netcup, Hetzner, DigitalOcean...), câu hỏi quen thuộc luôn là: Làm sao để vừa ẩn IP thật của VPS, vừa có HTTPS chuẩn chỉnh cho cả domain chính lẫn subdomain, lại không phải lo gia hạn chứng chỉ SSL mỗi 90 ngày?
Câu trả lời gọn gàng và bền vững nhất hiện nay là kết hợp Cloudflare Proxy (đám mây cam) + Cloudflare Origin Certificate (hạn 15 năm) ở chế độ Full (Strict) + Nginx.
Trong bài viết này, mình sẽ ghi lại toàn bộ quy trình thực chiến mình vừa triển khai cho tên miền toannh8.com và subdomain manga.toannh8.com trên một VPS Debian chạy Nginx — bao gồm cả 2 "cú vấp" kỹ thuật rất hay gặp ngoài thực tế (lỗi HTTP 525 và bẫy mất Security Headers trong Nginx).
Lưu ý bảo mật: Trong toàn bộ bài viết và ảnh minh họa, địa chỉ IP thật của VPS đã được thay bằng IP ví dụ chuẩn tài liệu
203.0.113.10. Khi áp dụng, bạn nhớ thay bằng IP thật của VPS mình nhé.
Mục lục
- Kiến trúc tổng quan
- Bước 1: Tạo Cloudflare Origin Certificate (Wildcard 15 năm)
- Bước 2: Chuẩn bị VPS và lưu chứng chỉ SSL
- Bước 3: Cấu hình Nginx cho domain chính & subdomain
- Bước 4: Deploy nhanh bằng
rsync - Bước 5: Trỏ DNS Proxy & bật SSL Full (Strict) trên Cloudflare
- Bước 6: Kiểm tra kết quả
- 2 bài học thực chiến đáng nhớ (HTTP 525 & bẫy
add_header)
1. Kiến trúc tổng quan
Khi bật Cloudflare Proxy (đám mây cam), mọi truy cập từ người dùng sẽ đi qua 2 chặng mã hóa HTTPS độc lập:

- Người dùng → Cloudflare Edge: Mã hóa bằng Cloudflare Universal SSL (miễn phí, tự động cấp wildcard cho
toannh8.comvà*.toannh8.com). - Cloudflare Edge → Nginx trên VPS: Mã hóa bằng Cloudflare Origin Certificate ở chế độ Full (Strict) — Cloudflare xác thực chặt chẽ chứng chỉ trên VPS để chống tấn công xen giữa (MITM).
Ưu điểm lớn của mô hình này:
- Ẩn hoàn toàn IP thật của VPS khi tra cứu DNS.
- Không cần cài Certbot / gia hạn Let's Encrypt mỗi 90 ngày: Origin Certificate có hạn tới 15 năm.
- Một chứng chỉ wildcard dùng chung cho mọi subdomain cấp 1: Tạo thêm
manga.toannh8.com,api.toannh8.com,blog.toannh8.comđều dùng lại đúng 1 cặp file.cert.pemvà.key.pem.
Mẹo thứ tự thao tác để không dính lỗi SSL tạm thời: Hãy tạo Origin Certificate và cấu hình xong Nginx trên VPS trước, rồi cuối cùng mới trỏ bản ghi DNS trên Cloudflare. Làm ngược lại sẽ khiến domain tạm thời rơi vào virtual host mặc định trên VPS và báo lỗi 525/526.
2. Bước 1: Tạo Cloudflare Origin Certificate (Wildcard 15 năm)
Đăng nhập dash.cloudflare.com → chọn tên miền toannh8.com:
- Vào menu trái SSL/TLS → Origin Server → nhấn Create Certificate.
- Giữ các thiết lập mặc định:
- Generate private key and CSR with Cloudflare
- Private key type:
RSA (2048) - Hostnames:
toannh8.comvà*.toannh8.com - Certificate Validity:
15 years
- Nhấn Create và copy lại 2 khối nội dung:
- Origin Certificate (
-----BEGIN CERTIFICATE----- ...) - Private Key (
-----BEGIN PRIVATE KEY----- ...) — chỉ hiển thị duy nhất 1 lần, hãy lưu cẩn thận.
- Origin Certificate (

3. Bước 2: Chuẩn bị VPS và lưu chứng chỉ SSL
SSH vào VPS của bạn và thực hiện 3 việc: cài rsync (để deploy file nhanh gọn), lưu cặp file SSL và tạo thư mục web tách biệt cho 2 site.
# 1. Cài rsync trên VPS (nếu chưa có)
sudo apt update && sudo apt install -y rsync
# 2. Tạo thư mục chứa chứng chỉ Cloudflare và dán nội dung từ Bước 1
sudo mkdir -p /etc/ssl/cloudflare
sudo nano /etc/ssl/cloudflare/toannh8.com.cert.pem
sudo nano /etc/ssl/cloudflare/toannh8.com.key.pem
# 3. Phân quyền chặt cho chứng chỉ & private key
sudo chmod 644 /etc/ssl/cloudflare/toannh8.com.cert.pem
sudo chmod 600 /etc/ssl/cloudflare/toannh8.com.key.pem
# 4. Tạo thư mục web riêng cho từng site (gán quyền user hiện tại để deploy không cần sudo)
sudo mkdir -p /var/www/toannh8.com /var/www/manga.toannh8.com
sudo chown -R $USER:$USER /var/www/toannh8.com /var/www/manga.toannh8.com
4. Bước 3: Cấu hình Nginx cho domain chính & subdomain
Chúng ta tách riêng cấu hình Nginx cho từng site trong /etc/nginx/sites-available/ để dễ bảo trì về sau:
4.1. Cấu hình toannh8.com (/etc/nginx/sites-available/toannh8.com.conf)
- Redirect 301 toàn bộ
http://vàhttps://www.toannh8.comvề URL chuẩn duy nhấthttps://toannh8.com. - Dùng HSTS mức an toàn (
max-age=3600, chưa bậtincludeSubDomains; preloadngay để tránh khóa cứng các subdomain thử nghiệm sau này).
# 1. Redirect all HTTP traffic (apex and www) directly to HTTPS apex
server {
listen 80;
listen [::]:80;
server_name toannh8.com www.toannh8.com;
return 301 https://toannh8.com$request_uri;
}
# 2. Redirect HTTPS www to HTTPS apex
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name www.toannh8.com;
ssl_certificate /etc/ssl/cloudflare/toannh8.com.cert.pem;
ssl_certificate_key /etc/ssl/cloudflare/toannh8.com.key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
return 301 https://toannh8.com$request_uri;
}
# 3. Main HTTPS server block (Cloudflare Origin SSL)
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name toannh8.com;
root /var/www/toannh8.com;
index index.html;
# Cloudflare Origin CA certificate
ssl_certificate /etc/ssl/cloudflare/toannh8.com.cert.pem;
ssl_certificate_key /etc/ssl/cloudflare/toannh8.com.key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Logs
access_log /var/log/nginx/toannh8_access.log;
error_log /var/log/nginx/toannh8_error.log warn;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=3600" always;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript image/svg+xml;
location / {
try_files $uri $uri/ =404;
}
# Disable caching for index.html (use expires -1 to preserve server-level security headers)
location = /index.html {
expires -1;
}
# Block access to hidden files (.git, .env, etc.)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
4.2. Cấu hình subdomain manga.toannh8.com (/etc/nginx/sites-available/manga.toannh8.com.conf)
Subdomain dùng lại chính cặp file chứng chỉ wildcard /etc/ssl/cloudflare/toannh8.com.cert.pem và .key.pem:
# 1. Redirect HTTP to HTTPS
server {
listen 80;
listen [::]:80;
server_name manga.toannh8.com;
return 301 https://manga.toannh8.com$request_uri;
}
# 2. Main HTTPS server block (Cloudflare Wildcard Origin SSL)
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name manga.toannh8.com;
root /var/www/manga.toannh8.com;
index index.html;
# Shared Cloudflare Origin CA wildcard certificate (toannh8.com & *.toannh8.com)
ssl_certificate /etc/ssl/cloudflare/toannh8.com.cert.pem;
ssl_certificate_key /etc/ssl/cloudflare/toannh8.com.key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Logs
access_log /var/log/nginx/manga_toannh8_access.log;
error_log /var/log/nginx/manga_toannh8_error.log warn;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=3600" always;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript image/svg+xml;
location / {
try_files $uri $uri/ =404;
}
location = /index.html {
expires -1;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
4.3. Kích hoạt cấu hình và reload Nginx
sudo ln -sf /etc/nginx/sites-available/toannh8.com.conf /etc/nginx/sites-enabled/toannh8.com.conf
sudo ln -sf /etc/nginx/sites-available/manga.toannh8.com.conf /etc/nginx/sites-enabled/manga.toannh8.com.conf
sudo nginx -t && sudo systemctl reload nginx
5. Bước 4: Deploy nhanh bằng rsync
So với việc dùng scp hay tar qua SSH (luôn gửi lại toàn bộ thư mục và không tự dọn file cũ), rsync -avz --delete chỉ truyền phần thay đổi và ghi đè an toàn (atomic).
Một mẹo nhỏ nhưng cực kỳ quan trọng khi viết deploy.sh có cờ --delete: luôn cố định thư mục nguồn bằng SCRIPT_DIR (tuyệt đối không dùng ./), tránh trường hợp chạy script từ nhầm thư mục và xóa sạch web trên VPS.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
rsync -avz --delete \
--chmod=Du=rwx,Dg=rx,Do=rx,Fu=rw,Fg=r,Fo=r \
--exclude 'nginx/' \
--exclude 'blog/' \
--exclude 'deploy.sh' \
--exclude '.git/' \
--exclude '.env*' \
--exclude '.DS_Store' \
"$SCRIPT_DIR/" my-vps:/var/www/toannh8.com/
echo "Deployed toannh8.com!"
Cấp quyền chạy và deploy:
chmod +x deploy.sh
./deploy.sh
6. Bước 5: Trỏ DNS Proxy & bật SSL Full (Strict) trên Cloudflare
Bây giờ VPS và Nginx đã sẵn sàng, ta quay lại Cloudflare Dashboard của toannh8.com để mở lưu lượng:
6.1. Bật SSL/TLS Full (Strict)
- Vào SSL/TLS → Overview → chọn Custom SSL/TLS → Full (Strict) → Save.
- Vào SSL/TLS → Edge Certificates → bật Always Use HTTPS và Automatic HTTPS Rewrites.

(Không chọn Flexible vì Nginx đã có redirect 301 HTTP → HTTPS; nếu dùng Flexible sẽ gây vòng lặp ERR_TOO_MANY_REDIRECTS.)
6.2. Tạo bản ghi DNS & bật Proxy (đám mây cam)
Vào DNS → Records → Add record, tạo 3 bản ghi:
| Type | Name | Content | Proxy status | Ghi chú |
|---|---|---|---|---|
A |
@ |
203.0.113.10 |
Proxied (cam) | Trỏ domain gốc toannh8.com |
CNAME |
www |
toannh8.com |
Proxied (cam) | Theo IP domain gốc, Nginx sẽ redirect 301 về apex |
A |
manga |
203.0.113.10 |
Proxied (cam) | Trỏ thẳng subdomain manga.toannh8.com |

Lưu ý về
wwwcho subdomain: Không nên tạowww.manga.toannh8.comvì đó là subdomain cấp 2 (*.*.toannh8.com), vượt quá phạm vi wildcard miễn phí*.toannh8.comcủa Cloudflare Universal SSL và sẽ gây lỗi chứng chỉ trên trình duyệt.
7. Bước 6: Kiểm tra kết quả
Chạy nhanh vài lệnh dig và curl từ máy cá nhân để xác nhận toàn bộ luồng hoạt động chuẩn chỉnh:
# 1. Xác nhận DNS trả về IP của Cloudflare (ẩn hoàn toàn IP VPS thật)
dig +short A toannh8.com
dig +short A manga.toannh8.com
# 2. Xác nhận redirect 301 từ HTTP và www về https://toannh8.com/
curl -sI http://toannh8.com | grep -iE '^(HTTP|location|cf-ray)'
curl -sI https://www.toannh8.com | grep -iE '^(HTTP|location|cf-ray)'
# 3. Xác nhận HTTPS 200 OK + đầy đủ Security Headers + Cache-Control: no-cache
curl -sI https://toannh8.com | grep -iE '^(HTTP|server|cf-ray|cache-control|strict-transport-security|x-frame-options|x-content-type-options)'
curl -sI https://manga.toannh8.com | grep -iE '^(HTTP|server|cf-ray|cache-control|strict-transport-security|x-frame-options|x-content-type-options)'
Kết quả đạt chuẩn sẽ trông như sau:
HTTP/2 200
server: cloudflare
cache-control: no-cache
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
referrer-policy: strict-origin-when-cross-origin
permissions-policy: camera=(), microphone=(), geolocation=()
strict-transport-security: max-age=3600
cf-ray: ...-SIN
8. 2 bài học thực chiến đáng nhớ (HTTP 525 & bẫy add_header)
Trong quá trình triển khai thực tế, có 2 lỗi kinh điển cực kỳ đáng ghi nhớ nếu bạn tự cấu hình Nginx + Cloudflare:
Bài học 1: Bẫy mất sạch Security Headers khi viết add_header trong location
Trong Nginx, lệnh add_header tuân theo quy tắc "được ăn cả, ngã về không":
Nếu bạn đặt dù chỉ 1 dòng
add_headerbên trong một blocklocation, Nginx sẽ xóa sạch toàn bộadd_headerkế thừa từ cấpservercho request đó.
Ví dụ cách viết bị lỗi rất phổ biến:
server {
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Strict-Transport-Security "max-age=3600" always;
location = /index.html {
expires -1;
# Dòng này vô tình làm MẤT HẾT X-Frame-Options & HSTS ở trên!
add_header Cache-Control "no-store, no-cache";
}
}
Cách sửa gọn nhất: Bỏ add_header Cache-Control đi, chỉ giữ expires -1; — Nginx sẽ tự sinh header Cache-Control: no-cache mà không phá vỡ kế thừa Security Headers ở cấp server.
Bài học 2: Lỗi HTTP 525 (SSL Handshake Failed) dù gọi trực tiếp VPS vẫn 200 OK
Khi mình chạy thử lần đầu, gọi trực tiếp curl --resolve toannh8.com:443:203.0.113.10 https://toannh8.com trả về 200 OK, nhưng đi qua Cloudflare lại báo 525 (ngay cả khi thử chuyển sang chế độ Full).
Nguyên nhân: Các vhost khác trên cùng VPS đều khai báo tường minh ssl_protocols, ssl_ciphers và ssl_prefer_server_ciphers, trong khi file cấu hình mới lại bỏ qua 3 dòng này để kế thừa mặc định từ nginx.conf. Sau khi khai báo đồng bộ đủ 3 dòng:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
và sudo systemctl reload nginx, cả toannh8.com lẫn manga.toannh8.com lập tức lên HTTP/2 200 OK ở chế độ Full (Strict).
Tổng kết
Chỉ với một chứng chỉ Cloudflare Origin CA wildcard duy nhất kết hợp Nginx và rsync, bạn đã có một nền tảng triển khai web trên VPS:
- Ẩn IP thật, chống DDoS cơ bản qua Cloudflare Proxy.
- HTTPS mã hóa 2 chiều Full (Strict), không phải lo gia hạn chứng chỉ suốt 15 năm.
- Thêm subdomain mới chỉ mất chưa tới 2 phút (thêm 1 bản ghi DNS + 1 file Nginx dùng chung chứng chỉ wildcard).
Chúc bạn cấu hình thành công!