---
title: "PostgreSQL pgvector: Hướng dẫn cài đặt chuẩn HTTPS và Nginx Reverse Proxy trên VPS NVMe"
description: "Hướng dẫn triển khai PostgreSQL tích hợp extension pgvector trên VPS NVMe, đặt API RAG phía sau Nginx Reverse Proxy chuẩn HTTPS Let's Encrypt an toàn."
canonical: "https://thuecloud.vn/postgresql-pgvector-huong-dan-cai-dat-chuan-https-va-nginx-reverse-proxy-tren-vps-nvme"
markdown_url: "https://thuecloud.vn/postgresql-pgvector-huong-dan-cai-dat-chuan-https-va-nginx-reverse-proxy-tren-vps-nvme.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:13.431Z"
updated_at: "2026-11-22T16:18:12.867Z"
site: "ThueCloud.Vn"
estimated_tokens: 1858
---
# PostgreSQL pgvector: Hướng dẫn cài đặt chuẩn HTTPS và Nginx Reverse Proxy trên VPS NVMe

> Hướng dẫn triển khai PostgreSQL tích hợp extension pgvector trên VPS NVMe, đặt API RAG phía sau Nginx Reverse Proxy chuẩn HTTPS Let's Encrypt an toàn.

- Canonical HTML: https://thuecloud.vn/postgresql-pgvector-huong-dan-cai-dat-chuan-https-va-nginx-reverse-proxy-tren-vps-nvme
- Markdown URL: https://thuecloud.vn/postgresql-pgvector-huong-dan-cai-dat-chuan-https-va-nginx-reverse-proxy-tren-vps-nvme.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

- Bước 1: Triển khai pgvector an toàn bằng Docker Compose
- Bước 2: Cấu hình Nginx Reverse Proxy chuẩn HTTPS cho API RAG
  - Bảng đối chiếu chỉ mục IVFFlat vs HNSW trong pgvector
- Câu hỏi thường gặp (FAQ)
  - Nếu cần kết nối từ máy cá nhân vào pgvector để quản trị dữ liệu thì làm sao khi cổng 5432 không mở?
  - Tạo chỉ mục HNSW trên bảng có 500.000 dòng cần lưu ý thông số nào của PostgreSQL?
  - Nên chọn pgvector hay Qdrant khi mới bắt đầu làm RAG?

**PostgreSQL + `pgvector`** là lựa chọn lý tưởng cho các đội ngũ muốn lưu trữ cả dữ liệu quan hệ truyền thống lẫn vector embedding cho AI/RAG trên cùng một cơ sở dữ liệu duy nhất trên [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam). Tuy nhiên, để hệ thống sẵn sàng phục vụ ứng dụng web hoặc webhook bên ngoài, bạn cần giữ cổng PostgreSQL `5432` hoàn toàn riêng tư nội bộ và đưa tầng API truy vấn qua **Nginx Reverse Proxy chuẩn HTTPS**.

> **Tóm tắt nhanh (TL;DR):**
> - Dùng image chính thức `pgvector/pgvector:pg16` trong Docker và chỉ bind ở mạng nội bộ (`internal: true`), không mở cổng `5432` ra Internet.
> - Tạo chỉ mục `HNSW` với `vector_cosine_ops` để tăng tốc độ tìm kiếm ngữ nghĩa gấp 10–50 lần trên ổ NVMe.
> - Đặt lớp API (FastAPI/Go/n8n) sau Nginx Reverse Proxy có HTTPS (Let's Encrypt) và header bảo mật chuẩn.
> - *Cập nhật: 11/2026*

## Bước 1: Triển khai `pgvector` an toàn bằng Docker Compose

```yaml
services:
  pgvector:
    image: pgvector/pgvector:pg16
    container_name: pgvector-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: rag_user
      POSTGRES_PASSWORD: ${PG_PASSWORD}
      POSTGRES_DB: rag_db
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - rag_internal

volumes:
  pgdata:

networks:
  rag_internal:
    driver: bridge
    internal: true
```

Kích hoạt extension và tạo bảng mẫu kèm chỉ mục HNSW:

```sql
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE documents (
  id bigserial PRIMARY KEY,
  content text NOT NULL,
  embedding vector(1536)
);

CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);
```

## Bước 2: Cấu hình Nginx Reverse Proxy chuẩn HTTPS cho API RAG

Tạo file `/etc/nginx/sites-available/rag-api.conf`, kết hợp chứng chỉ Let's Encrypt và tăng `proxy_read_timeout` lên `120s` để không bị ngắt kết nối khi LLM sinh câu trả lời dài:

```nginx
server {
    listen 443 ssl http2;
    server_name rag.example.com;

    ssl_certificate /etc/letsencrypt/live/rag.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/rag.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 120s;
    }
}
```

### Bảng đối chiếu chỉ mục IVFFlat vs HNSW trong `pgvector`

| Loại chỉ mục | Tốc độ truy vấn | Độ chính xác (Recall) | Khuyến nghị dùng trên VPS NVMe |
|---|---|---|---|
| `HNSW` | Rất nhanh | Cao (95–99%) | Ưu tiên dùng cho hệ thống production |
| `IVFFlat` | Khá nhanh | Trung bình | Phù hợp khi RAM rất hạn chế, dữ liệu ít đổi |

Để bảo vệ lớp API này khỏi truy cập trái phép và tối ưu kết nối ngoài, xem thêm [cài đặt Authelia 2FA chuẩn production](https://thuecloud.vn/bao-mat-voi-authelia-2fa-huong-dan-cai-dat-va-kich-hoat-quy-tac-bao-ve-chuan-production), [tạo script healthcheck IP sống](https://thuecloud.vn/huong-dan-haproxy-tcp-tao-script-healthcheck-tu-dong-kiem-tra-ip-song), [mở port 1433 SQL Server chỉ cho IP riêng](https://thuecloud.vn/windows-vps-mo-port-1433-sql-server-chi-cho-phep-ip-rieng) hoặc [giải pháp AI & Proxy USA](https://thuecloud.vn/proxy-usa) tại [ThueCloud.Vn](https://thuecloud.vn/giai-phap).

> **Chạy PostgreSQL pgvector siêu tốc trên NVMe tại ThueCloud.Vn:**
> **KVM Enterprise NVMe** tại `vn-hn` (VAT `0%`, SLA `99,9%`, hoàn tiền `07 ngày`). **VPS VN 1** (`45.000đ`–`60.000đ`), **VPS VN 2** (`78.000đ`–`90.000đ`), **VPS VN 3** (`139.000đ`–`170.000đ/tháng`). Xem chi tiết tại [bảng giá](https://thuecloud.vn/pricing).

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

### Nếu cần kết nối từ máy cá nhân vào `pgvector` để quản trị dữ liệu thì làm sao khi cổng `5432` không mở?
Bạn sử dụng **SSH Tunnel** (`ssh -L 5432:127.0.0.1:5432 user@IP_VPS`) rồi kết nối DBeaver/pgAdmin vào `localhost:5432` rất an toàn.

### Tạo chỉ mục HNSW trên bảng có 500.000 dòng cần lưu ý thông số nào của PostgreSQL?
Nên tăng tạm `maintenance_work_mem` (ví dụ `SET maintenance_work_mem = '1GB';`) trước khi chạy `CREATE INDEX ... USING hnsw` để quá trình dựng index diễn ra nhanh gấp nhiều lần.

### Nên chọn `pgvector` hay Qdrant khi mới bắt đầu làm RAG?
Nếu dữ liệu dưới 1 triệu vector và bạn đã quen thuộc với SQL/PostgreSQL, `pgvector` giúp giảm bớt 1 dịch vụ phải vận hành và dễ dàng JOIN trực tiếp dữ liệu nghiệp vụ.

## References / Sources

- https://github.com/pgvector/pgvector
- https://nginx.org/en/docs/http/ngx_http_proxy_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)
