Website được thiết kế tối ưu cho thành viên chính thức. Hãy Đăng nhập hoặc Đăng ký để truy cập đầy đủ nội dung và chức năng. Nội dung bạn cần không thấy trên website, có thể do bạn chưa đăng nhập. Nếu là thành viên của website, bạn cũng có thể yêu cầu trong nhóm Zalo "CNTT" các nội dung bạn quan tâm.

Bài 11.2. Chuẩn hóa template “HTTP + HTTPS” cho 3 nhóm (website thường / web app / websocket-api)

ICT

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_namebackend.

  • Mọi site đều có ACME challenge, redirect HTTP→HTTPS, snippets dùng chung.

Ba nhóm template chuẩn:

  1. Website thường (CMS/website cơ sở y tế/chuyên ngành thông tin).

  2. Web app (QLCL/KHTH/Nhân sự/Thiết bị… timeout dài, upload lớn).

  3. 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/_letsencrypt

  • Snippet: /etc/nginx/snippets/letsencrypt-acme.conf

2.2. Snippets chuẩn đã có

  • snippets/ssl/ssl-common.conf

  • snippets/ssl/ssl-params.conf

  • snippets/headers/security-headers.conf

  • snippets/proxy/proxy-common.conf

  • snippets/proxy/proxy-webapp.conf

  • snippets/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>.conf

  • Chuyên ngành: /etc/nginx/sites-available/chuyen-nganh/<domain>.conf

Template (copy dùng ngay)

##
# TEMPLATE: WEBSITE-STD (HTTP+HTTPS)
# Type: Public website / CMS
# Backend: QMS/AI (internal)
##
# ---- HTTP 80: ACME + redirect to HTTPS ----
server {
   listen 80;
   server_name example.com www.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 example.com www.example.com;
   ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/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;
   location / {
       proxy_pass http://10.10.10.66;  # QMS primary
   }
}

 

Khi cần tăng upload cho website CMS
  • 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.conf nế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.conf là 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):

location /socket/ {
   include snippets/proxy/proxy-websocket.conf;
   proxy_pass http://10.10.10.66;
}


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 đó:

 
nginx -t && systemctl reload nginx

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

  1. Tạo file .conf theo đúng nhóm (website/webapp/ws).

  2. Thay server_name.

  3. Thay đường dẫn cert đúng domain trong ssl_certificate.

  4. Gán backend đúng (QMS/AI).

  5. Symlink sang sites-enabled.

  6. Kiểm tra:

    • nginx -t

    • systemctl reload nginx

  7. 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.