Xget 部署指南
全面介绍Xget在Cloudflare Workers、EdgeOne Pages、Vercel、Netlify、Deno Deploy、Docker和Podman等多种平台的部署方案。本文涵盖各平台的前置要求、配置步骤、环境变量设置以及常见问题排查指南
概述
Xget 支持多种部署方式,包括 Cloudflare Workers、EdgeOne Pages、Vercel、Netlify、Deno Deploy、Docker 和 Podman。本文档详细介绍了各种部署方式的步骤和配置。
前置要求
通用要求
- Node.js 18+ 或兼容的 JavaScript 运行环境
- Git 客户端
- 基本的命令行操作能力
特定平台要求
Cloudflare Workers
- Cloudflare 账户(免费计划即可)
- Wrangler CLI 工具
EdgeOne Pages
- 腾讯云账户
- EdgeOne 服务
Vercel
- Vercel 账户
- Vercel CLI 工具
Netlify
- Netlify 账户
- Netlify CLI 工具
Deno Deploy
- Deno 账户
- Deno CLI 工具
Docker/Podman
- Docker 或 Podman 运行时
- 服务器或本地机器
部署到 Cloudflare Workers
方式一:使用 Wrangler CLI
1. 安装 Wrangler CLI
npm install -g wrangler
2. 登录 Cloudflare
wrangler login
3. 克隆 Xget 仓库
git clone https://github.com/xixu-me/Xget.git
cd Xget
4. 配置 Wrangler
创建 wrangler.toml 文件:
name = "xget"
main = "src/index.js"
compatibility_date = "2024-01-01"
[vars]
# 自定义配置变量
# CUSTOM_DOMAIN = "your-domain.com"
# KV 命名空间(可选)
[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-namespace-id"
5. 部署
wrangler deploy
6. 验证部署
curl https://xget.your-subdomain.workers.dev/gh/microsoft/vscode/archive/main.zip
方式二:使用 Cloudflare Dashboard
1. 登录 Cloudflare Dashboard
2. 创建 Worker
- 点击 “Workers & Pages”
- 点击 “Create Application”
- 选择 “Create Worker”
- 输入 Worker 名称
- 点击 “Deploy”
3. 上传代码
- 在 Worker 编辑器中粘贴 Xget 代码
- 点击 “Save and Deploy”
4. 配置自定义域名(可选)
- 在 Worker 设置中点击 “Triggers”
- 点击 “Custom Domains”
- 添加自定义域名
配置选项
环境变量
在 wrangler.toml 中配置:
[vars]
# 缓存时长(秒)
CACHE_TTL = "1800"
# 最大重试次数
MAX_RETRIES = "3"
# 请求超时(毫秒)
REQUEST_TIMEOUT = "30000"
KV 存储
创建 KV 命名空间:
wrangler kv:namespace create "CACHE"
将返回的 ID 添加到 wrangler.toml。
自定义域名
wrangler domains add your-domain.com
部署到 EdgeOne Pages
1. 准备代码
git clone https://github.com/xixu-me/Xget.git
cd Xget
2. 构建项目
npm install
npm run build
3. 登录腾讯云
4. 创建站点
- 点击 “站点列表”
- 点击 “新建站点”
- 输入域名信息
- 选择 “静态网站” 或 “Serverless”
5. 上传代码
方式一:Git 仓库
- 在站点设置中选择 “源站设置”
- 添加 Git 仓库
- 配置构建命令和输出目录
方式二:直接上传
- 将构建后的文件打包
- 在控制台上传文件包
6. 配置边缘函数
- 在站点设置中选择 “边缘函数”
- 创建新的边缘函数
- 粘贴 Xget 代码
- 配置路由规则
7. 验证部署
curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
部署到 Vercel
1. 安装 Vercel CLI
npm install -g vercel
2. 登录 Vercel
vercel login
3. 克隆仓库
git clone https://github.com/xixu-me/Xget.git
cd Xget
4. 部署
vercel
按照提示选择配置选项。
5. 配置环境变量
在 Vercel Dashboard 中添加环境变量:
CACHE_TTL: 1800MAX_RETRIES: 3REQUEST_TIMEOUT: 30000
6. 生产部署
vercel --prod
7. 验证部署
curl https://your-project.vercel.app/gh/microsoft/vscode/archive/main.zip
部署到 Netlify
1. 安装 Netlify CLI
npm install -g netlify-cli
2. 登录 Netlify
netlify login
3. 克隆仓库
git clone https://github.com/xixu-me/Xget.git
cd Xget
4. 初始化项目
netlify init
按照提示选择配置选项。
5. 配置 Netlify Functions
创建 netlify.toml 文件:
[build]
functions = "netlify/functions"
[functions]
node_bundler = "esbuild"
6. 部署
netlify deploy --prod
7. 验证部署
curl https://your-site.netlify.app/gh/microsoft/vscode/archive/main.zip
部署到 Deno Deploy
1. 安装 Deno CLI
# 使用安装脚本(Linux/macOS)
curl -fsSL https://deno.land/install.sh | sh
# 使用 Homebrew(macOS)
brew install deno
# 使用 Chocolatey(Windows)
choco install deno
2. 克隆仓库
git clone https://github.com/xixu-me/Xget.git
cd Xget
3. 适配 Deno
将代码适配为 Deno 兼容格式:
// 使用 Deno 标准库
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
async function handler(req) {
// Xget 核心逻辑
}
serve(handler);
4. 登录 Deno Deploy
访问 Deno Deploy 并登录。
5. 创建新项目
- 点击 “New Project”
- 选择 “Deploy from GitHub”
- 选择 Xget 仓库
- 配置入口文件
- 点击 “Deploy”
6. 配置环境变量
在项目设置中添加环境变量。
7. 验证部署
curl https://your-project.deno.dev/gh/microsoft/vscode/archive/main.zip
自托管部署(Docker)
1. 准备 Dockerfile
创建 Dockerfile:
# 使用 Node.js 18 作为基础镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
# 复制 package 文件
COPY package*.json ./
# 安装依赖
RUN npm ci --only=production
# 复制源代码
COPY . .
# 暴露端口
EXPOSE 3000
# 启动应用
CMD ["node", "src/index.js"]
2. 构建 Docker 镜像
docker build -t xget:latest .
3. 运行容器
docker run -d \
--name xget \
-p 3000:3000 \
-e CACHE_TTL=1800 \
-e MAX_RETRIES=3 \
-e REQUEST_TIMEOUT=30000 \
xget:latest
4. 使用 Docker Compose
创建 docker-compose.yml:
version: '3.8'
services:
xget:
build: .
container_name: xget
ports:
- "3000:3000"
environment:
- CACHE_TTL=1800
- MAX_RETRIES=3
- REQUEST_TIMEOUT=30000
restart: unless-stopped
networks:
- xget-network
networks:
xget-network:
driver: bridge
启动服务:
docker-compose up -d
5. 配置反向代理(Nginx)
创建 Nginx 配置:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
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;
}
}
重启 Nginx:
sudo nginx -t
sudo systemctl restart nginx
6. 配置 SSL(Let’s Encrypt)
使用 Certbot:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
7. 验证部署
curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
自托管部署(Podman)
1. 安装 Podman
# Ubuntu/Debian
sudo apt install podman
# Fedora
sudo dnf install podman
# Arch Linux
sudo pacman -S podman
2. 准备 Containerfile
创建 Containerfile(Podman 使用 Containerfile 而非 Dockerfile):
# 使用 Node.js 18 作为基础镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
# 复制 package 文件
COPY package*.json ./
# 安装依赖
RUN npm ci --only=production
# 复制源代码
COPY . .
# 暴露端口
EXPOSE 3000
# 启动应用
CMD ["node", "src/index.js"]
3. 构建镜像
podman build -t xget:latest .
4. 运行容器
podman run -d \
--name xget \
-p 3000:3000 \
-e CACHE_TTL=1800 \
-e MAX_RETRIES=3 \
-e REQUEST_TIMEOUT=30000 \
xget:latest
5. 使用 Podman Compose
安装 Podman Compose:
pip install podman-compose
创建 docker-compose.yml(与 Docker 相同):
version: '3.8'
services:
xget:
build: .
container_name: xget
ports:
- "3000:3000"
environment:
- CACHE_TTL=1800
- MAX_RETRIES=3
- REQUEST_TIMEOUT=30000
restart: unless-stopped
networks:
- xget-network
networks:
xget-network:
driver: bridge
启动服务:
podman-compose up -d
6. 配置系统服务
创建 systemd 服务文件 /etc/systemd/system/xget.service:
[Unit]
Description=Xget Service
After=network.target
[Service]
Type=forking
User=podman
Group=podman
ExecStart=/usr/bin/podman-compose -f /path/to/docker-compose.yml up -d
ExecStop=/usr/bin/podman-compose -f /path/to/docker-compose.yml down
Restart=always
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable xget
sudo systemctl start xget
7. 验证部署
curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
配置优化
性能优化
1. 缓存配置
Cloudflare Workers
const cache = caches.default;
async function cachedFetch(request) {
const cacheKey = new Request(request.url, request);
const cached = await cache.match(cacheKey);
if (cached) {
return cached;
}
const response = await fetch(request);
const clonedResponse = response.clone();
await cache.put(cacheKey, clonedResponse);
return response;
}
自托管
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 1800 });
2. 压缩配置
// 启用压缩
const compression = require('compression');
app.use(compression());
3. 连接池配置
const http = require('http');
const https = require('https');
const agent = new https.Agent({
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 10
});
安全配置
1. CORS 配置
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
2. 速率限制
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 分钟
max: 1000 // 每个窗口期最多 1000 个请求
});
app.use(limiter);
3. 安全标头
const helmet = require('helmet');
app.use(helmet());
监控配置
1. 日志记录
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
2. 性能监控
const promClient = require('prom-client');
const httpRequestDuration = new promClient.Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'route', 'code']
});
故障排查
常见问题
1. 部署失败
Cloudflare Workers
- 检查
wrangler.toml配置 - 确认账户权限
- 查看部署日志
Docker
- 检查 Dockerfile 语法
- 确认端口映射
- 查看容器日志
2. 性能问题
- 检查缓存配置
- 优化压缩设置
- 调整连接池大小
3. 安全问题
- 检查 CORS 配置
- 验证速率限制
- 审查安全标头
调试技巧
1. 启用调试日志
const DEBUG = process.env.DEBUG === 'true';
function debug(message) {
if (DEBUG) {
console.log(`[DEBUG] ${message}`);
}
}
2. 性能分析
const start = Date.now();
// 执行操作
const duration = Date.now() - start;
console.log(`Operation took ${duration}ms`);
3. 错误追踪
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
维护指南
更新部署
Cloudflare Workers
git pull
wrangler deploy
Docker
git pull
docker-compose down
docker-compose build
docker-compose up -d
备份配置
# 备份配置文件
cp wrangler.toml wrangler.toml.backup
cp docker-compose.yml docker-compose.yml.backup
监控健康
# 检查服务状态
curl https://your-domain.com/health
# 检查容器状态
docker ps
# 检查 Worker 状态
wrangler tail
最佳实践
1. 使用环境变量
避免硬编码配置,使用环境变量:
const config = {
cacheTTL: process.env.CACHE_TTL || 1800,
maxRetries: process.env.MAX_RETRIES || 3,
requestTimeout: process.env.REQUEST_TIMEOUT || 30000
};
2. 实现健康检查
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: Date.now() });
});
3. 优雅关闭
process.on('SIGTERM', () => {
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
4. 版本管理
使用语义化版本号:
v1.0.0 - 初始版本
v1.0.1 - 补丁更新
v1.1.0 - 次要更新
v2.0.0 - 主要更新
总结
Xget 支持多种部署方式,选择合适的部署方式取决于你的需求:
- Cloudflare Workers:推荐用于生产环境,性能最佳
- EdgeOne Pages:适合国内用户,访问速度快
- Vercel/Netlify:易于部署,适合个人项目
- Deno Deploy:现代化部署,支持 TypeScript
- Docker/Podman:自托管,完全控制
无论选择哪种部署方式,都建议:
- 配置适当的缓存策略
- 启用安全防护
- 实施监控和日志
- 定期更新和维护