---
title: "Triển khai Astro SSR (Node Adapter + Nginx): thiết lập CI/CD tự động build và rollback không downtime"
description: "Hướng dẫn triển khai Astro SSR với Node Adapter sau Nginx trên Cloud VPS: thiết lập GitHub Actions CI/CD, PM2/systemd reload và rollback không downtime."
canonical: "https://thuecloud.vn/trien-khai-astro-ssr-node-adapter-nginx-thiet-lap-ci-cd-tu-dong-build-va-rollback-khong-downtime"
markdown_url: "https://thuecloud.vn/trien-khai-astro-ssr-node-adapter-nginx-thiet-lap-ci-cd-tu-dong-build-va-rollback-khong-downtime.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:23.284Z"
updated_at: "2026-11-22T16:18:22.184Z"
site: "ThueCloud.Vn"
estimated_tokens: 2176
---
# Triển khai Astro SSR (Node Adapter + Nginx): thiết lập CI/CD tự động build và rollback không downtime

> Hướng dẫn triển khai Astro SSR với Node Adapter sau Nginx trên Cloud VPS: thiết lập GitHub Actions CI/CD, PM2/systemd reload và rollback không downtime.

- Canonical HTML: https://thuecloud.vn/trien-khai-astro-ssr-node-adapter-nginx-thiet-lap-ci-cd-tu-dong-build-va-rollback-khong-downtime
- Markdown URL: https://thuecloud.vn/trien-khai-astro-ssr-node-adapter-nginx-thiet-lap-ci-cd-tu-dong-build-va-rollback-khong-downtime.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

- Cấu trúc thư mục Release và Rollback không downtime
- Bảng thông số dịch vụ và cổng kết nối chuẩn
- Cấu hình Systemd và Nginx Reverse Proxy
- Câu hỏi thường gặp (FAQ)
  - Nên dùng mode: 'standalone' hay middleware cho Astro Node Adapter?
  - Làm sao để rollback nhanh nếu bản build mới bị lỗi giao diện hoặc crash?
  - VPS 1GB RAM có chạy nổi Astro SSR không?

Astro kết hợp **Node Adapter (`@astrojs/node`)** cho phép bạn vừa giữ tốc độ HTML tĩnh siêu nhanh ở các trang tĩnh, vừa render dữ liệu động server-side (SSR) cho blog, trang giá hoặc SEO theo thời gian thực. Tuy nhiên, nếu mỗi lần deploy bạn chạy `npm run build` trực tiếp trên thư mục đang chạy, người dùng sẽ gặp lỗi 502 trong lúc build. Bài viết này hướng dẫn triển khai Astro SSR chuẩn Production trên [Cloud VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) với cơ chế chuyển mạch symlink và rollback tức thì không downtime.

> **Tóm tắt nhanh (TL;DR):**
> - Build ứng dụng Astro trên **GitHub Actions** hoặc thư mục release tách biệt, không build đè lên thư mục đang phục vụ traffic.
> - Dùng cấu trúc thư mục `releases/<timestamp>` và chuyển `current` symlink để **rollback về bản cũ trong dưới 2 giây**.
> - Đặt **Nginx làm Reverse Proxy** phía trước Node Adapter (chạy ở chế độ `standalone` qua `systemd`) để xử lý SSL, Gzip/Brotli và static assets.
> - *Cập nhật: 11/2026*

## Cấu trúc thư mục Release và Rollback không downtime

Thay vì pull code trực tiếp vào `/var/www/astro`, hãy tổ chức thư mục trên VPS theo chuẩn Capistrano/atomic deploy:

```text
/var/www/astro-app/
├── releases/
│   ├── 20261122_1000/
│   └── 20261122_1430/
├── shared/
│   └── .env
└── current -> /var/www/astro-app/releases/20261122_1430
```

Khi có phiên bản mới, CI/CD giải nén bản build `dist/` vào thư mục release mới, liên kết file `.env` từ `shared/`, kiểm tra healthcheck rồi mới đổi symlink `current` và restart dịch vụ `systemd` (hoặc dùng Blue-Green 2 cổng `4321`/`4322` trước Nginx để đạt 0ms gián đoạn). Nếu phát hiện lỗi sau deploy, bạn chỉ cần trỏ symlink `current` về release trước đó và restart service.

## Bảng thông số dịch vụ và cổng kết nối chuẩn

| Thành phần | Vai trò | Cấu hình khuyến nghị |
| :--- | :--- | :--- |
| **Astro Node Adapter** | Render SSR HTML tại `127.0.0.1:4321` | `mode: 'standalone'`, quản lý bởi `systemd` |
| **Nginx Reverse Proxy** | Terminate SSL TLS 1.3, nén Brotli/Gzip | Proxy pass vào `127.0.0.1:4321`, cache `/_astro/*` 1 năm |
| **GitHub Actions** | Chạy `pnpm build`, đóng gói artifact qua SSH/SCP | Giữ lại 5 bản release gần nhất trên VPS |
| **Healthcheck** | Kiểm tra HTTP `200` trước khi chuyển traffic | `curl -fsS http://127.0.0.1:4321/healthz` |

## Cấu hình Systemd và Nginx Reverse Proxy

Tạo file `/etc/systemd/system/astro-ssr.service` để Node.js tự khởi động lại nếu gặp sự cố:

```ini
[Unit]
Description=Astro SSR Standalone Server
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/astro-app/current
EnvironmentFile=/var/www/astro-app/shared/.env
Environment=HOST=127.0.0.1
Environment=PORT=4321
ExecStart=/usr/bin/node /var/www/astro-app/current/dist/server/entry.mjs
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
```

Trên Nginx, cấu hình cache dài hạn cho tài nguyên tĩnh `/_astro/` nhưng giữ `no-store` hoặc cache ngắn cho HTML động:

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

    location /_astro/ {
        alias /var/www/astro-app/current/dist/client/_astro/;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location / {
        proxy_pass http://127.0.0.1:4321;
        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;
    }
}
```

Để tối ưu thêm tầng truyền tải mạng khi người dùng truy cập từ di động, bạn có thể tham khảo bài [cài đặt QUIC Congestion Control](https://thuecloud.vn/quic-congestion-control-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te). Ngoài ra, nếu hệ thống của bạn có kết hợp luồng AI phía sau, xem thêm [triển khai n8n + Claude trên Cloud VPS](https://thuecloud.vn/trien-khai-ai-automation-nganh-logistics-kho-van-bang-n8n-claude-tren-cloud-vps) hoặc cân nhắc vị trí datacenter qua bài [so sánh VPS Hà Nội vs TP.HCM](https://thuecloud.vn/so-sanh-vps-ha-noi-vs-vps-tp-hcm-2026-hieu-nang-chi-phi-va-kich-ban-nen-chon). Tham khảo thêm kiến trúc tổng thể tại [Giải pháp ThueCloud](https://thuecloud.vn/giai-phap) và [bảng giá Cloud VPS](https://thuecloud.vn/pricing).

> **Khởi chạy Astro SSR mượt mà trên Cloud VPS ThueCloud.Vn:**
> - **VPS VN 1** (`45.000đ`/tháng theo năm hoặc `60.000đ`/tháng): Chạy tốt Astro SSR cho blog cá nhân hoặc landing page doanh nghiệp.
> - **VPS VN 2** (`78.000đ`/tháng theo năm hoặc `90.000đ`/tháng): Dư dả RAM cho Astro SSR + Redis cache + build trực tiếp khi cần.
> - KVM Enterprise NVMe tại `vn-hn`, **VAT 0%**, **SLA 99,9%**, **hoàn tiền 07 ngày**.

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

### Nên dùng `mode: 'standalone'` hay `middleware` cho Astro Node Adapter?
Với mô hình chạy độc lập sau Nginx Reverse Proxy, `mode: 'standalone'` là đơn giản và ổn định nhất vì Astro tự tạo HTTP server sẵn sàng chạy ngay bằng `node dist/server/entry.mjs` mà không cần viết thêm Express/Fastify wrapper.

### Làm sao để rollback nhanh nếu bản build mới bị lỗi giao diện hoặc crash?
Với cấu trúc thư mục `releases/`, bạn chỉ cần chạy `ln -sfn /var/www/astro-app/releases/<ban_cu> /var/www/astro-app/current && sudo systemctl restart astro-ssr`. Toàn bộ quá trình mất chưa tới 2 giây.

### VPS 1GB RAM có chạy nổi Astro SSR không?
Hoàn toàn chạy được nếu bạn build ứng dụng trên GitHub Actions rồi mới đẩy thư mục `dist/` lên VPS. Quá trình runtime của Astro Node Adapter chỉ tiêu tốn khoảng 80-150MB RAM tùy lượng traffic đồng thời.

## References / Sources

- https://docs.astro.build/en/guides/integrations-guide/node/
- https://docs.github.com/en/actions
- 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)
