---
title: "Astro Hybrid SSR Node: Cài đặt chi tiết từ đầu và kiểm tra hoạt động thực tế"
description: "Hướng dẫn cài đặt Astro Hybrid SSR với adapter @astrojs/node trên VPS NVMe: cấu hình standalone server, systemd service, Nginx reverse proxy và kiểm thử HTML."
canonical: "https://thuecloud.vn/astro-hybrid-ssr-node-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te"
markdown_url: "https://thuecloud.vn/astro-hybrid-ssr-node-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:37.440Z"
updated_at: "2026-11-22T16:18:36.558Z"
site: "ThueCloud.Vn"
estimated_tokens: 2079
---
# Astro Hybrid SSR Node: Cài đặt chi tiết từ đầu và kiểm tra hoạt động thực tế

> Hướng dẫn cài đặt Astro Hybrid SSR với adapter @astrojs/node trên VPS NVMe: cấu hình standalone server, systemd service, Nginx reverse proxy và kiểm thử HTML.

- Canonical HTML: https://thuecloud.vn/astro-hybrid-ssr-node-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te
- Markdown URL: https://thuecloud.vn/astro-hybrid-ssr-node-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te.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ài đặt @astrojs/node và cấu hình Hybrid SSR
- Cấu hình Systemd và Nginx Reverse Proxy trên VPS
- Kiểm tra hoạt động SSR thực tế và tối ưu thêm
- Câu hỏi thường gặp (FAQ)
  - Sự khác biệt giữa output: 'server' và prerender = false trong Astro là gì?
  - Tại sao nên để Nginx phục vụ thư mục /_astro/ thay vì đẩy hết qua Node.js?
  - Cần làm gì nếu Astro SSR bị lỗi 502 Bad Gateway sau khi deploy bản build mới?

Kiến trúc mặc định của Astro xuất ra tệp tĩnh (SSG) rất nhanh, nhưng với các trang cần dữ liệu thời gian thực từ cơ sở dữ liệu (như bảng giá động, bài viết CMS cập nhật tức thì hoặc kiểm tra phiên đăng nhập), bạn cần bật chế độ **Astro Hybrid SSR** dùng adapter `@astrojs/node` trên [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam). Mô hình này giữ nguyên tốc độ tĩnh cho trang ít đổi và chỉ render server-side các route động.

> **Tóm tắt nhanh (TL;DR):**
> - **Cài đặt adapter:** Thêm `@astrojs/node` với `mode: 'standalone'` và khai báo `export const prerender = false` trên từng route cần SSR.
> - **Vận hành Production:** Chạy `node ./dist/server/entry.mjs` dưới tiến trình `systemd`, kết hợp Nginx làm reverse proxy phục vụ file tĩnh `/_astro/*`.
> - **Kiểm tra thực tế:** Dùng `curl -i` kiểm tra HTML trả về trực tiếp từ server mà không cần chờ JavaScript hydrate.
> - *Cập nhật: 11/2026*

## 1. Cài đặt @astrojs/node và cấu hình Hybrid SSR

Trong thư mục dự án Astro (phiên bản Astro 5+ hỗ trợ mặc định static kèm on-demand rendering khi có adapter), cài đặt adapter Node chính thức:

```bash
npx astro add node
```

Cập nhật tệp `astro.config.mjs` sử dụng chế độ `standalone` để Astro tự đóng gói máy chủ HTTP gọn nhẹ sẵn sàng chạy trên VPS:

```javascript
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';

export default defineConfig({
  output: 'server', // Hoặc giữ static và đặt prerender = false cho từng route động
  adapter: node({
    mode: 'standalone',
  }),
});
```

Nếu bạn muốn trang chủ tĩnh hoàn toàn và chỉ render SSR cho trang bài viết `/blog/[slug].astro`, hãy đặt `export const prerender = true;` ở các trang tĩnh và `export const prerender = false;` ở các trang động, sau đó chạy `npm run build`.

## 2. Cấu hình Systemd và Nginx Reverse Proxy trên VPS

Tạo dịch vụ `systemd` để đảm bảo tiến trình Node.js tự khởi động lại khi gặp sự cố:

```ini
# /etc/systemd/system/astro-ssr.service
[Unit]
Description=Astro Hybrid SSR Server
After=network.target

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

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

Tại Nginx, hãy chuyển tiếp request động vào cổng `4321` và phục vụ trực tiếp thư mục `/_astro/` từ ổ NVMe để đạt TTFB tối ưu:

```nginx
server {
    listen 80;
    server_name example.vn;

    location /_astro/ {
        alias /var/www/astro-app/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-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

## 3. Kiểm tra hoạt động SSR thực tế và tối ưu thêm

Chạy `curl -s http://127.0.0.1:4321/blog/bai-viet-moi | head -n 30` để xác minh toàn bộ thẻ `<title>`, `<meta name="description">` và nội dung chính đã nằm sẵn trong HTML trả về từ server. Bạn có thể đo thêm tốc độ bằng [bộ công cụ kiểm tra web](https://thuecloud.vn/tools) và đọc thêm [xử lý lỗi VcXsrv X11](https://thuecloud.vn/vcxsrv-x11-forwarding-phan-tich-loi-thuong-gap-va-quy-trinh-xu-ly-su-co-nhanh) hoặc [tinh chỉnh Windmill Scripts](https://thuecloud.vn/windmill-python-ts-scripts-tinh-chinh-tham-so-hieu-nang-va-gioi-han-tai-nguyen-production).

| Hạng mục kiểm tra | Lệnh xác minh | Kết quả đạt chuẩn |
| :--- | :--- | :--- |
| Trạng thái service | `systemctl status astro-ssr` | `active (running)` trên `127.0.0.1:4321` |
| Render HTML phía server | `curl -s http://127.0.0.1:4321/` | Có đầy đủ nội dung HTML, không rỗng |
| Tài sản tĩnh `/_astro/` | `curl -I https://example.vn/_astro/...` | HTTP `200`, `Cache-Control: immutable` |
| Header Reverse Proxy | Kiểm tra log app | Nhận đúng `X-Forwarded-Proto: https` |

> **Triển khai Astro SSR siêu mượt trên KVM NVMe của ThueCloud.Vn:**
> - Hạ tầng tại `vn-hn`, **SLA 99,9%**, **VAT 0%**, **hoàn tiền 07 ngày**.
> - Gói **VPS VN 1** (`45.000đ`/`60.000đ`), **VPS VN 2** (`78.000đ`/`90.000đ`), **VPS VN 3** (`139.000đ`/`170.000đ`) tại [Bảng giá VPS](https://thuecloud.vn/pricing) giúp phản hồi SSR dưới 20ms.

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

### Sự khác biệt giữa output: 'server' và prerender = false trong Astro là gì?
Từ Astro 5 trở đi, mô hình hybrid được gộp chung: nếu bạn chọn adapter Node, bạn có thể để mặc định tĩnh và gắn `export const prerender = false` ở từng trang cần SSR, hoặc đặt `output: 'server'` để mọi trang đều SSR trừ trang có `prerender = true`.

### Tại sao nên để Nginx phục vụ thư mục /_astro/ thay vì đẩy hết qua Node.js?
Nginx đọc file tĩnh trực tiếp từ ổ NVMe nhanh hơn nhiều và tiêu tốn ít RAM/CPU hơn hẳn Node.js, giúp tiến trình `entry.mjs` tập trung toàn lực cho việc render HTML động.

### Cần làm gì nếu Astro SSR bị lỗi 502 Bad Gateway sau khi deploy bản build mới?
Hãy kiểm tra xem bạn đã chạy `npm run build` xong trước khi gọi `sudo systemctl restart astro-ssr` chưa, và xem chi tiết lỗi qua `journalctl -u astro-ssr -n 50`.

## References / Sources

- https://docs.astro.build/en/guides/integrations-guide/node/
- https://docs.astro.build/en/guides/on-demand-rendering/
- 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)
