本文引用的文件列表
本文件面向AI有声书生成平台的运维与开发人员,系统化梳理Nginx反向代理配置,重点解析以下方面:
本仓库包含两套Nginx配置入口:
生产环境:book.rrbrr.com.conf提供静态资源、API代理、证书与安全头等完整配置;另有部署脚本与文档中给出的Nginx示例。
graph TB
subgraph "Docker开发环境"
DConf["docker-nginx/bookapi.conf"]
DCmp["docker-nginx/docker-compose.yml"]
DReadme["docker-nginx/README.md"]
end
subgraph "生产环境"
PConf["book.rrbrr.com.conf"]
PDeploy["deploy.sh"]
PDoc["docs/DEPLOY.md"]
end
DCmp --> DConf
DReadme --> DConf
PDoc --> PConf
PDeploy --> PConf
图表来源
Section sources
Section sources
下图展示了从客户端到后端服务的典型请求路径,以及静态资源与API代理的关键节点。
sequenceDiagram
participant U as "用户浏览器"
participant N as "Nginx(80/443)"
participant S as "静态资源(/uploads,/audio,*.ext)"
participant A as "API代理(/api/)"
participant B as "后端服务(3000/3100)"
U->>N : "HTTP/HTTPS 请求"
alt "静态资源"
N->>S : "命中静态location"
S-->>U : "带Cache-Control/Expires的静态文件"
else "API请求"
N->>A : "转发到后端"
A->>B : "反向代理"
B-->>A : "响应"
A-->>U : "返回API结果"
end
图表来源
docker-compose
映射80:80,挂载bookapi.conf,extra_hosts注入host.docker.internal
flowchart TD
Start(["请求进入"]) --> Port80["监听80端口"]
Port80 --> ProxyPass["location / 反向代理到 host.docker.internal:3000"]
ProxyPass --> Headers["设置X-Forwarded-*头部"]
Headers --> Backend["到达后端服务(3000)"]
Backend --> Resp["返回响应"]
Resp --> End(["结束"])
图表来源
Section sources
SPA回退
/路由使用try_files回退到index.html
flowchart TD
A["80端口"] --> R301["301重定向到https"]
B["443端口"] --> SSL["加载证书与加密套件"]
SSL --> Static["静态资源缓存策略"]
SSL --> API["/api/代理到后端"]
SSL --> SPA["SPA回退到index.html"]
Static --> Done["完成"]
API --> Done
SPA --> Done
图表来源
Section sources
SPA回退
/路由使用try_files回退到index.html,适配Vue单页应用
flowchart TD
Req["请求静态资源"] --> Match{"匹配目录或扩展名"}
Match --> |/uploads/*| U["设置7天缓存与immutable"]
Match --> |/audio/*| Au["设置7天缓存与immutable"]
Match --> |*.ext(js/css/...)| Ext["设置1年缓存与immutable"]
Match --> |其他| Fallback["SPA回退index.html"]
U --> Resp["返回带缓存头的静态文件"]
Au --> Resp
Ext --> Resp
Fallback --> Resp
图表来源
Section sources
错误页:497重定向至HTTPS
sequenceDiagram
participant C as "客户端"
participant N as "Nginx 80"
participant N443 as "Nginx 443"
C->>N : "HTTP请求"
N-->>C : "301重定向到https"
C->>N443 : "HTTPS请求"
N443-->>C : "返回受保护内容(含安全头)"
图表来源
Section sources
生产环境
设置Host、X-Real-IP、X-Forwarded-For、X-Forwarded-Proto
sequenceDiagram
participant C as "客户端"
participant N as "Nginx"
participant B as "后端服务"
C->>N : "请求 /api/*"
N->>B : "转发到后端(3000/3100)"
B-->>N : "响应"
N-->>C : "返回API结果"
图表来源
Section sources
[本节为概念性说明,不直接分析具体源码文件]
生产环境
Nginx直接代理到本地后端进程(127.0.0.1:3100)
graph LR
Client["客户端"] --> Nginx["Nginx(80/443)"]
Nginx --> Static["静态资源(/uploads,/audio,*.ext)"]
Nginx --> API["API代理(/api/)"]
API --> Backend["后端服务(3000/3100)"]
图表来源
Section sources
Section sources
Section sources
[本节为总结性内容,不直接分析具体源码文件]
Section sources
Section sources