Files
happy-life-star/docs/nginx-config-changes/2026-07-21-shortnovel-sse-optimization.md
T

31 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
author: AI Assistant
created_at: 2026-07-21
purpose: 记录 nginx 配置优化,禁用 SSE 响应缓冲
---
# nginx 配置优化 - ShortNovel SSE
**文件:** `/etc/nginx/sites-enabled/lifescript.happylifeos.com.conf`
**变更内容:**
`location /api/shortNovel/` 块中使用以下配置:
- `proxy_http_version 1.1;` - 确保 SSE 长连接使用 HTTP/1.1
- `proxy_buffering off;` - 禁用代理缓冲
- `chunked_transfer_encoding on;` - 启用分块传输编码
- `proxy_set_header Connection "keep-alive";` - 保持上游连接
**原因:**
- nginx 默认启用 `proxy_buffering on`,会缓冲后端响应
- 对于 SSEServer-Sent Events)长连接,缓冲会导致事件无法立即到达前端
- 禁用缓冲后,每个事件都能立即转发给客户端,实现真正的流式传输
**验证:**
- nginx 语法检查通过:`nginx -t` → syntax is ok / test is successful
- nginx 重载成功:worker 进程已更新
- 使用 `curl -N https://lifescript.happylifeos.com/api/shortNovel/stream` 测试 SSE 流式传输
**备份文件:** `/etc/nginx/sites-enabled/lifescript.happylifeos.com.conf.backup.20260721_233252`
**管理方式:** 宝塔面板,通过 `/etc/init.d/nginx reload` 重载配置