本文档介绍如何使用 Cloudflare 或 CloudFront 等 CDN 服务加速 Nexus Terminal 的静态资源分发。
Nexus Terminal 的前端构建产物(Vite + Brotli/Gzip 预压缩)适合通过 CDN 进行边缘缓存。静态资源(JS/CSS/图片)可长期缓存,而 API 和 WebSocket 连接需要直通到后端。
在 Rules > Cache Rules 中添加:
| 规则名称 | 匹配条件 | 缓存行为 |
|---|---|---|
| 静态资源缓存 | URI Path matches /assets/* | Cache Everything, Edge TTL: 1 year |
| JS/CSS 缓存 | URI Path matches /*.js 或 *.css | Cache Everything, Edge TTL: 1 year |
| HTML 绕过 | URI Path matches /index.html | Bypass Cache |
| API 绕过 | URI Path starts with /api/ | Bypass Cache |
| WebSocket 直通 | URI Path starts with /ws/ | Bypass Cache |
如果使用免费计划,可使用 Page Rules(最多 3 条免费):
*example.com/assets/*
Cache Level: Cache Everything
Edge Cache TTL: 1 monthapi.example.com)| 设置项 | 值 |
|---|---|
| Origin Domain | api.example.com |
| Origin Path | 留空 |
| Origin Protocol Policy | HTTPS Only |
| Origin Keep-Alive Timeout | 60 seconds |
创建多个 Behavior 以区分缓存策略:
| 设置项 | 值 |
|---|---|
| Path Pattern | /assets/* |
| Viewer Protocol Policy | Redirect HTTP to HTTPS |
| Allowed HTTP Methods | GET, HEAD |
| Cache Policy | 使用 Managed-CachingOptimized |
| Compress Objects Automatically | Yes |
| 设置项 | 值 |
|---|---|
| Path Pattern | /ws/* |
| Viewer Protocol Policy | Redirect HTTP to HTTPS |
| Allowed HTTP Methods | GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE |
| Cache Policy | Use Legacy Cache Policy (转发所有 headers, cookies, query strings) |
| Origin Request Policy | AllViewer |
| Compress Objects Automatically | No |
| 设置项 | 值 |
|---|---|
| Path Pattern | /api/* |
| Viewer Protocol Policy | Redirect HTTP to HTTPS |
| Allowed HTTP Methods | GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE |
| Cache Policy | Use Legacy Cache Policy (转发所有 headers, cookies, query strings) |
| Origin Request Policy | AllViewer |
| Compress Objects Automatically | No |
| 设置项 | 值 |
|---|---|
| Path Pattern | Default (*) |
| Viewer Protocol Policy | Redirect HTTP to HTTPS |
| Cache Policy | Managed-CachingDisabled(HTML 不缓存) |
| Compress Objects Automatically | Yes |
配置 404 错误响应返回 /index.html(SPA 路由支持):
| Error Code | Response Code | Response Page Path |
|---|---|---|
| 404 | 200 | /index.html |
在 CDN 后面部署时,需要更新 Nginx 配置:
# Cloudflare IP 范围(定期更新)
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
# 使用 CDN 传递的真实 IP
real_ip_header CF-Connecting-IP;# 告诉 CDN 根据 Accept-Encoding 缓存不同版本
add_header Vary "Accept-Encoding" always;# 检查是否安装了 brotli 模块
# nginx -V 2>&1 | grep brotli
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;# 检查响应头是否包含 CDN 标识
curl -I https://example.com/
# Cloudflare 应包含:
# server: cloudflare
# cf-cache-status: HIT/MISS/DYNAMIC
# CloudFront 应包含:
# x-amz-cf-id: ...
# x-amz-cf-pop: ...# 测试 WebSocket 连接
wscat -c wss://example.com/ws/
# 或使用浏览器 DevTools > Network > WS 标签# 检查静态资源缓存
curl -I https://example.com/assets/index-xxx.js
# 应包含:
# cache-control: public, max-age=31536000, immutable
# cf-cache-status: HIT (CloudFront: x-cache: Hit from cloudfront)proxy_set_header Upgrade 和 proxy_set_header Connection 配置/assets/* 路径cache-control 响应头是否正确