1. Mục tiêu của bài 11.2
Khi hệ thống có hàng trăm domain, việc “mỗi site một kiểu” sẽ tạo ra 3 vấn đề vận hành:
Không đồng nhất cấu hình (TLS, headers, proxy headers, timeout…).
Khó bàn giao, khó audit, khó sửa hàng loạt.
Khi failover QMS ↔ AI dễ sai sót vì cấu trúc file không giống nhau.
Do đó, cần chuẩn hóa 3 template chuẩn để:
Copy dùng ngay khi thêm domain mới.
Chỉ thay server_name và backend.
Mọi site đều có ACME challenge, redirect HTTP→HTTPS, snippets dùng chung.
Ba nhóm template chuẩn:
Website thường (CMS/website cơ sở y tế/chuyên ngành thông tin).
Web app (QLCL/KHTH/Nhân sự/Thiết bị… timeout dài, upload lớn).
WebSocket-API (realtime, dashboard, socket, API cần upgrade).
2. Quy ước chung trước khi dùng template
2.1. Webroot ACME dùng chung (đã chuẩn hóa ở Bài 11.1)
Thư mục:
/var/www/_letsencryptSnippet:
/etc/nginx/snippets/letsencrypt-acme.conf
2.2. Snippets chuẩn đã có
snippets/ssl/ssl-common.confsnippets/ssl/ssl-params.confsnippets/headers/security-headers.confsnippets/proxy/proxy-common.confsnippets/proxy/proxy-webapp.confsnippets/proxy/proxy-websocket.conf(cho WebSocket)
Lưu ý: Các template dưới đây giả định đang dùng kịch bản B: Certbot chỉ cấp/renew cert; file Nginx do mình quản trị theo template.
3. TEMPLATE 1 — Website thường (CMS / website cơ sở y tế / website chuyên ngành)
Áp dụng khi
Website giới thiệu, tin tức, CMS.
Không cần WebSocket.
Upload không quá lớn (hoặc có thể tăng nhẹ nếu cần).
Đặt file tại (theo nhóm):
Cơ sở y tế:
/etc/nginx/sites-available/yte-co-so/<domain>.confChuyên ngành:
/etc/nginx/sites-available/chuyen-nganh/<domain>.conf
Template (copy dùng ngay)
Thêm (hoặc dùng snippet riêng) trong HTTPS server block:
client_max_body_size 50M;hoặc theo nhu cầu.
4. TEMPLATE 2 — Web App (QLCL/KHTH/Nhân sự/Thiết bị…) — timeout dài, upload lớn
Áp dụng khi
Ứng dụng quản trị, dashboard, hệ thống điều hành.
Có upload file lớn (report, excel, pdf…).
Thường gặp request chạy lâu.
Đặt file tại
/etc/nginx/sites-available/webapp/<app-name>-app.conf
Template (copy dùng ngay)
##
# TEMPLATE: WEBAPP (HTTP+HTTPS)
# Type: Web application (long timeout, large upload)
# Backend: QMS/AI (internal)
##
# ---- HTTP 80: ACME + redirect to HTTPS ----
server {
listen 80;
server_name app.example.com;
include snippets/letsencrypt-acme.conf;
return 301 https://$host$request_uri;
}
# ---- HTTPS 443: reverse proxy to backend ----
server {
listen 443 ssl http2;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
include snippets/ssl/ssl-common.conf;
include snippets/ssl/ssl-params.conf;
include snippets/headers/security-headers.conf;
include snippets/proxy/proxy-common.conf;
include snippets/proxy/proxy-webapp.conf;
location / {
proxy_pass http://10.10.10.66; # QMS primary
}
}
Gợi ý thực tế
Nếu web app có đường dẫn upload đặc thù (ví dụ
/webform), có thể override theo location cụ thể, nhưng vẫn giữ template này là nền tảng.
5. TEMPLATE 3 — WebSocket / API realtime (Upgrade headers)
Áp dụng khi
Ứng dụng dùng WebSocket (realtime).
Dashboard có live updates.
API gateway cần keep-alive, streaming.
Đặt file tại
/etc/nginx/sites-available/webapp/<name>-ws.conf
hoặc/etc/nginx/sites-available/external/<name>-ws.confnếu backend ngoài.
Template (copy dùng ngay)
##
# TEMPLATE: WEBSOCKET-API (HTTP+HTTPS)
# Type: WebSocket / realtime API
# Backend: QMS/AI (internal)
##
# ---- HTTP 80: ACME + redirect to HTTPS ----
server {
listen 80;
server_name ws.example.com;
include snippets/letsencrypt-acme.conf;
return 301 https://$host$request_uri;
}
# ---- HTTPS 443: reverse proxy to backend with WebSocket support ----
server {
listen 443 ssl http2;
server_name ws.example.com;
ssl_certificate /etc/letsencrypt/live/ws.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ws.example.com/privkey.pem;
include snippets/ssl/ssl-common.conf;
include snippets/ssl/ssl-params.conf;
include snippets/headers/security-headers.conf;
include snippets/proxy/proxy-common.conf;
include snippets/proxy/proxy-webapp.conf; # timeouts, upload (if needed)
include snippets/proxy/proxy-websocket.conf; # Upgrade + Connection headers
location / {
proxy_pass http://10.10.10.66; # QMS primary
}
}
Lưu ý quan trọng
proxy-websocket.conflà bắt buộc để WebSocket “upgrade” thành công.Nếu ứng dụng websocket chạy trên path riêng (ví dụ
/socket), có thể chỉ bật websocket snippet cho location đó.
Ví dụ (tùy chọn):
6. Cơ chế đổi backend QMS ↔ AI trong mọi template
Để failover nhanh, trong mỗi template chỉ cần thay:
QMS primary:
proxy_pass http://10.10.10.66;
AI standby:
proxy_pass http://10.10.10.88;
Sau đó:
Do HTTPS terminate tại proxy nên không liên quan chứng chỉ.
7. Checklist chuẩn khi thêm domain mới
Tạo file
.conftheo đúng nhóm (website/webapp/ws).Thay
server_name.Thay đường dẫn cert đúng domain trong
ssl_certificate.Gán backend đúng (QMS/AI).
Symlink sang
sites-enabled.Kiểm tra:
nginx -tsystemctl reload nginx
Kiểm tra ACME:
curl http://domain/.well-known/acme-challenge/test.txt
Kết luận
Chuẩn hóa template “HTTP+HTTPS” theo 3 nhóm giúp hệ thống:
Đồng nhất cấu hình cho hàng trăm domain.
Tối ưu vận hành: thêm site nhanh, ít sai sót.
Failover QMS ↔ AI gọn và an toàn.
ACME challenge độc lập backend, tránh rủi ro renew cert thất bại.
- Đăng nhập để gửi ý kiến