---
title: "HestiaCP trên VPS: Cách cấu hình CORS header và bảo mật API"
description: "Hướng dẫn cấu hình CORS header chuẩn xác trên HestiaCP chạy Nginx và bảo mật endpoint API (Rate Limit, ẩn version, Security Headers) không bị mất khi panel cập nhật."
canonical: "https://thuecloud.vn/hestiacp-tren-vps-cach-cau-hinh-cors-header-va-bao-mat-api"
markdown_url: "https://thuecloud.vn/hestiacp-tren-vps-cach-cau-hinh-cors-header-va-bao-mat-api.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:14.922Z"
updated_at: "2026-11-22T16:18:12.867Z"
site: "ThueCloud.Vn"
estimated_tokens: 1920
---
# HestiaCP trên VPS: Cách cấu hình CORS header và bảo mật API

> Hướng dẫn cấu hình CORS header chuẩn xác trên HestiaCP chạy Nginx và bảo mật endpoint API (Rate Limit, ẩn version, Security Headers) không bị mất khi panel cập nhật.

- Canonical HTML: https://thuecloud.vn/hestiacp-tren-vps-cach-cau-hinh-cors-header-va-bao-mat-api
- Markdown URL: https://thuecloud.vn/hestiacp-tren-vps-cach-cau-hinh-cors-header-va-bao-mat-api.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Quy tắc cấu hình CORS an toàn cho API trên Nginx
- Cấu hình Nginx CORS Whitelist & Rate Limit trên HestiaCP
- Câu hỏi thường gặp (FAQ)
  - Tại sao tôi thêm add_header trong block location thì các header bảo mật ở block server bị mất?
  - Tham số always cuối dòng add_header có tác dụng gì?
  - Làm sao ẩn phiên bản Nginx và PHP để tăng bảo mật API?

Khi tách frontend (React/Astro/Next.js) và backend API (Laravel/WordPress REST/Node.js) trên hai tên miền khác nhau chạy trên [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) cài **HestiaCP**, trình duyệt sẽ chặn các yêu cầu gọi API nếu thiếu **CORS (Cross-Origin Resource Sharing)** header hợp lệ. Ngược lại, nếu cấu hình `Access-Control-Allow-Origin: *` một cách tùy tiện cho cả API có cookie/token, hệ thống lại đối mặt với lỗ hổng bảo mật nghiêm trọng.

> **Tóm tắt nhanh (TL;DR):**
> - Không đặt `Access-Control-Allow-Origin: *` kết hợp `Access-Control-Allow-Credentials: true`; hãy kiểm tra whitelist domain bằng biến `map $http_origin` trong Nginx.
> - Cấu hình bổ sung trong file `/home/<user>/conf/web/<domain>/nginx.ssl.conf_*` (hoặc custom web template) để HestiaCP không ghi đè khi rebuild.
> - Kết hợp thêm `limit_req_zone` (Rate Limiting) và Security Headers (`X-Content-Type-Options`, `X-Frame-Options`) để bảo vệ API.
> - *Cập nhật: 11/2026*

## Quy tắc cấu hình CORS an toàn cho API trên Nginx

| Tình huống API | Header `Allow-Origin` nên dùng | Có bật `Allow-Credentials`? | Xử lý `OPTIONS` Preflight |
| :--- | :--- | :--- | :--- |
| Public API đọc dữ liệu mở | `*` (chỉ khi không dùng cookie/auth) | Không (`false`) | Trả về `204 No Content` |
| API Frontend + Backend riêng domain | Whitelist cụ thể (`https://app.domain.com`) | Có (`true`) nếu dùng Cookie/Session | Bắt buộc trả `204` kèm header CORS |
| API Nội bộ / Server-to-Server | Không cần mở CORS | Không | Chặn theo IP hoặc API Key |

## Cấu hình Nginx CORS Whitelist & Rate Limit trên HestiaCP

HestiaCP tự động nạp mọi file có tiền tố `nginx.ssl.conf_` nằm trong thư mục cấu hình web của user. Trước hết, tạo file `/etc/nginx/conf.d/cors-map.conf` để định nghĩa whitelist origin an toàn:

```nginx
map $http_origin $cors_origin {
    default "";
    "~^https://(app|admin)\.tenmien\.com$" $http_origin;
}

# Giới hạn tốc độ gọi API: tối đa 15 request/giây mỗi IP
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=15r/s;
```

Tiếp theo, tạo file `/home/<user>/conf/web/<api-domain>/nginx.ssl.conf_custom` để áp dụng CORS, xử lý preflight `OPTIONS` và gắn Rate Limit cho các route `/api/`:

```nginx
location /api/ {
    limit_req zone=api_limit burst=30 nodelay;

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' $cors_origin always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Max-Age' 86400;
        return 204;
    }

    add_header 'Access-Control-Allow-Origin' $cors_origin always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'X-Content-Type-Options' 'nosniff' always;

    try_files $uri $uri/ /index.php?$args;
}
```

Kiểm tra cú pháp và nạp lại Nginx:

```bash
sudo nginx -t && sudo systemctl reload nginx
```

*Lưu ý:* Nếu backend (Laravel/Express) đã tự gắn header `Access-Control-Allow-Origin`, bạn **chỉ** cấu hình ở một nơi (Nginx hoặc Backend) để tránh lỗi trình duyệt báo `multiple values`.

Bạn có thể kiểm tra nhanh header trả về bằng `curl -I -X OPTIONS -H "Origin: https://app.tenmien.com" https://api.tenmien.com/api/health` hoặc dùng các [công cụ hỗ trợ](https://thuecloud.vn/tools) tại ThueCloud. Tham khảo thêm bài [khắc phục sự cố Systemd Service Failed to Start trên Debian 12](https://thuecloud.vn/khac-phuc-su-co-systemd-service-failed-to-start-tren-debian-12) nếu Nginx không reload được.

> **Vận hành Web & API tốc độ cao tại ThueCloud.Vn:**
> - Hạ tầng KVM Enterprise NVMe tại `vn-hn` cho độ trễ API dưới 15ms, VAT `0%`, SLA `99,9%`, hoàn tiền `07 ngày`.
> - Gói **VPS VN 1** (`45.000đ`/`60.000đ`) và **VPS VN 2** (`78.000đ`/`90.000đ`) chạy HestiaCP + Nginx API mượt mà.
> - Xem chi tiết tại [bảng giá ThueCloud](https://thuecloud.vn/pricing).

## Câu hỏi thường gặp (FAQ)

### Tại sao tôi thêm `add_header` trong block `location` thì các header bảo mật ở block `server` bị mất?
Đây là hành vi mặc định của Nginx: khi một block con (`location`) khai báo bất kỳ chỉ thị `add_header` nào, nó sẽ không kế thừa các `add_header` từ block cha (`server`). Hãy khai báo đầy đủ các header cần thiết trong cùng block `location` đó.

### Tham số `always` cuối dòng `add_header` có tác dụng gì?
Mặc định Nginx chỉ gắn `add_header` cho các mã HTTP thành công (2xx, 3xx). Thêm `always` giúp header CORS vẫn được trả về kể cả khi API báo lỗi `400`, `401` hay `500`, giúp frontend đọc được thông báo lỗi thay vì bị trình duyệt che bằng lỗi CORS.

### Làm sao ẩn phiên bản Nginx và PHP để tăng bảo mật API?
Trong `/etc/nginx/nginx.conf`, đặt `server_tokens off;`, và trong `php.ini` đặt `expose_php = Off`.

## References / Sources

- https://hestiacp.com/docs/server-administration/web-templates.html
- https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
- https://nginx.org/en/docs/http/ngx_http_limit_req_module.html

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
