Skip to content

更多部署平台

除了常见的 Vercel、Netlify、GitHub Pages,还有许多新兴的部署平台提供独特的优势。本教程介绍 Railway、Render、Zeabur、Fly.io 等平台的部署方法。

平台对比

平台免费额度国内访问特点适合场景
Railway$5/月额度一般支持数据库、后台服务全栈应用
Render750小时/月一般自动 SSL、PR 预览静态站点、Web 服务
Zeabur有免费额度国内优化、一键部署国内用户优先
Fly.io3 个小实例一般边缘部署、Docker需要边缘计算
Cloudflare Pages免费额度大方全球 CDN、Workers高流量站点

Railway 部署

特点

  • 支持 Docker 部署
  • 内置数据库(PostgreSQL、MySQL、Redis)
  • 支持后台服务
  • 提供 $5/月免费额度

部署步骤

1. 创建项目

bash
# 安装 Railway CLI
npm install -g @railway/cli

# 登录
railway login

# 初始化项目
railway init

2. 配置构建命令

创建 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.html

2. 通过 Dashboard 部署

  1. 访问 Render Dashboard
  2. 点击 New +Static Site
  3. 连接 Git 仓库
  4. 配置:
    • Build Command: npm run build
    • Publish Directory: docs/.vitepress/dist
  5. 点击 Create Static Site

配置自定义域名

  1. 在站点设置中添加自定义域名
  2. 在域名服务商配置 CNAME 指向 Render 提供的地址
  3. Render 自动配置 SSL

Zeabur 部署

特点

  • 国内访问优化
  • 一键部署
  • 支持 Docker 和源码部署
  • 提供免费额度

部署步骤

1. 安装 CLI

bash
npm install -g zeabur

2. 登录并部署

bash
# 登录
zeabur auth login

# 部署
zeabur deploy

3. 或通过 Web 控制台

  1. 访问 Zeabur Dashboard
  2. 创建新项目
  3. 添加服务 → Git 服务
  4. 选择仓库,自动检测 VitePress
  5. 点击部署

配置文件

创建 zeabur.yaml(可选):

yaml
# zeabur.yaml
services:
  - name: docs
    type: static
    buildCommand: npm run build
    outputDirectory: docs/.vitepress/dist

Fly.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 login

3. 创建 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 deploy

5. 配置 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,sin

Cloudflare Pages 部署

特点

  • 免费额度大方
  • 全球 CDN 加速
  • 国内访问较快
  • 支持 Cloudflare Workers

部署步骤

1. 通过 Git 集成

  1. 访问 Cloudflare Pages
  2. 创建项目 → 连接 Git
  3. 选择仓库
  4. 配置构建设置:
    • Framework preset: VitePress
    • Build command: npm run build
    • Build output directory: docs/.vitepress/dist

2. 直接上传

bash
# 安装 Wrangler
npm install -g wrangler

# 登录
wrangler login

# 部署
wrangler pages deploy docs/.vitepress/dist --project-name=your-project

添加自定义域名

  1. 在项目设置中添加自定义域名
  2. 如果域名已在 Cloudflare,自动配置
  3. 否则需添加 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

下一步

贡献者

加载中...

想要成为贡献者?

在 CNB 上参与贡献