更多部署平台
除了常见的 Vercel、Netlify、GitHub Pages,还有许多新兴的部署平台提供独特的优势。本教程介绍 Railway、Render、Zeabur、Fly.io 等平台的部署方法。
平台对比
| 平台 | 免费额度 | 国内访问 | 特点 | 适合场景 |
|---|---|---|---|---|
| Railway | $5/月额度 | 一般 | 支持数据库、后台服务 | 全栈应用 |
| Render | 750小时/月 | 一般 | 自动 SSL、PR 预览 | 静态站点、Web 服务 |
| Zeabur | 有免费额度 | 快 | 国内优化、一键部署 | 国内用户优先 |
| Fly.io | 3 个小实例 | 一般 | 边缘部署、Docker | 需要边缘计算 |
| Cloudflare Pages | 免费额度大方 | 快 | 全球 CDN、Workers | 高流量站点 |
Railway 部署
特点
- 支持 Docker 部署
- 内置数据库(PostgreSQL、MySQL、Redis)
- 支持后台服务
- 提供 $5/月免费额度
部署步骤
1. 创建项目
bash
# 安装 Railway CLI
npm install -g @railway/cli
# 登录
railway login
# 初始化项目
railway init2. 配置构建命令
创建 railway.json:
json
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "npm run preview",
"healthcheckPath": "/",
"healthcheckTimeout": 100,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}3. 配置 package.json
json
{
"scripts": {
"build": "vitepress build docs",
"preview": "npx serve docs/.vitepress/dist -p $PORT"
}
}4. 部署
bash
railway up使用 Dockerfile
dockerfile
# Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/docs/.vitepress/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]Render 部署
特点
- 免费 SSL 证书
- 自动 PR 预览
- 支持 Docker
- 免费额度:750 小时/月
部署步骤
1. 创建 render.yaml
yaml
# render.yaml
services:
- type: web
name: vitepress-docs
env: static
buildCommand: npm run build
staticPublishPath: docs/.vitepress/dist
pullRequestPreviewsEnabled: true
headers:
- path: /*
name: X-Frame-Options
value: DENY
routes:
- type: rewrite
source: /*
destination: /index.html2. 通过 Dashboard 部署
- 访问 Render Dashboard
- 点击
New +→Static Site - 连接 Git 仓库
- 配置:
- Build Command:
npm run build - Publish Directory:
docs/.vitepress/dist
- Build Command:
- 点击
Create Static Site
配置自定义域名
- 在站点设置中添加自定义域名
- 在域名服务商配置 CNAME 指向 Render 提供的地址
- Render 自动配置 SSL
Zeabur 部署
特点
- 国内访问优化
- 一键部署
- 支持 Docker 和源码部署
- 提供免费额度
部署步骤
1. 安装 CLI
bash
npm install -g zeabur2. 登录并部署
bash
# 登录
zeabur auth login
# 部署
zeabur deploy3. 或通过 Web 控制台
- 访问 Zeabur Dashboard
- 创建新项目
- 添加服务 → Git 服务
- 选择仓库,自动检测 VitePress
- 点击部署
配置文件
创建 zeabur.yaml(可选):
yaml
# zeabur.yaml
services:
- name: docs
type: static
buildCommand: npm run build
outputDirectory: docs/.vitepress/distFly.io 部署
特点
- 边缘计算平台
- 全球分布式部署
- 支持 Docker
- 3 个免费小实例
部署步骤
1. 安装 flyctl
bash
# macOS
brew install flyctl
# Linux
curl -L https://fly.io/install.sh | sh
# Windows
powershell -Command "iwr https://fly.io/install.ps1 -useb | iex"2. 登录
bash
fly auth login3. 创建 Dockerfile
dockerfile
# Dockerfile
# 构建阶段
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# 运行阶段
FROM node:20-alpine
WORKDIR /app
RUN npm install -g serve
COPY --from=builder /app/docs/.vitepress/dist ./dist
EXPOSE 8080
CMD ["serve", "-s", "dist", "-l", "8080"]4. 初始化并部署
bash
# 初始化(首次)
fly launch
# 部署
fly deploy5. 配置 fly.toml
toml
# fly.toml
app = "your-app-name"
primary_region = "nrt"
[build]
dockerfile = "Dockerfile"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
[[vm]]
memory = '256mb'
cpu_kind = 'shared'
cpus = 1多区域部署
bash
# 添加区域
fly regions add sin # 新加坡
fly regions add lax # 洛杉矶
# 扩展到多区域
fly scale count 2 --region nrt,sinCloudflare Pages 部署
特点
- 免费额度大方
- 全球 CDN 加速
- 国内访问较快
- 支持 Cloudflare Workers
部署步骤
1. 通过 Git 集成
- 访问 Cloudflare Pages
- 创建项目 → 连接 Git
- 选择仓库
- 配置构建设置:
- Framework preset:
VitePress - Build command:
npm run build - Build output directory:
docs/.vitepress/dist
- Framework preset:
2. 直接上传
bash
# 安装 Wrangler
npm install -g wrangler
# 登录
wrangler login
# 部署
wrangler pages deploy docs/.vitepress/dist --project-name=your-project添加自定义域名
- 在项目设置中添加自定义域名
- 如果域名已在 Cloudflare,自动配置
- 否则需添加 CNAME 记录
配置 Headers
创建 _headers 文件:
# public/_headers
/*
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
/assets/*
Cache-Control: public, max-age=31536000, immutable配置重定向
创建 _redirects 文件:
# public/_redirects
/old-path /new-path 301
/guide /guide/getting-started 302部署配置最佳实践
环境变量管理
yaml
# GitHub Actions 示例
env:
NODE_VERSION: '20'
# 构建时注入
VITE_API_URL: ${{ secrets.API_URL }}构建缓存优化
yaml
# GitHub Actions 缓存配置
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}健康检查配置
yaml
# Render 健康检查
healthCheckPath: /
healthCheckTimeout: 100
# Fly.io 健康检查
[checks]
[checks.alive]
type = "http"
port = 8080
path = "/"
interval = "10s"
timeout = "2s"CI/CD 集成
GitHub Actions 多平台部署
yaml
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
platform:
- name: cloudflare
run: |
npm install -g wrangler
wrangler pages deploy docs/.vitepress/dist --project-name=docs
- name: fly
run: |
fly deploy --remote-only
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Deploy to ${{ matrix.platform.name }}
run: ${{ matrix.platform.run }}
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}平台选择建议
| 需求 | 推荐平台 |
|---|---|
| 国内用户为主 | Cloudflare Pages、Zeabur |
| 需要后台服务 | Railway、Render、Fly.io |
| 需要数据库 | Railway(内置)、Render |
| 边缘计算 | Cloudflare Workers、Fly.io |
| 完全免费 | Cloudflare Pages、GitHub Pages |
下一步
- 学习 CI/CD 自动化部署 深入了解自动化流程
- 学习 性能优化 优化部署后的站点性能
- 学习 自定义域名配置 绑定自己的域名