docs: 移除过时的文档文件

清理 docs 目录下不再需要的文档内容。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-08 10:07:34 +08:00
parent d482e44bec
commit ae027b6218
6 changed files with 0 additions and 4177 deletions

View File

@ -1,194 +0,0 @@
# NGINX 文档汇总
本目录包含 NGINX 官方文档的深度总结,涵盖 NGINX 的所有常用功能。
## 文档索引
| 序号 | 文档 | 内容概述 |
|------|------|----------|
| 01 | [概述与基础指南](./01-nginx-overview.md) | NGINX 架构、配置结构、启动停止、信号控制、命令行参数 |
| 02 | [安装与构建指南](./02-nginx-installation.md) | Linux 包安装、源码编译、配置参数、依赖库、模块编译 |
| 03 | [HTTP 核心模块](./03-nginx-http-core.md) | server/location 配置、请求路由、文件服务、客户端控制、性能优化 |
| 04 | [反向代理与负载均衡](./04-nginx-proxy-loadbalancing.md) | proxy_pass、upstream、负载均衡算法、健康检查、缓存、WebSocket |
| 05 | [SSL/TLS 与 HTTPS](./05-nginx-ssl-https.md) | HTTPS 配置、SSL 指令、会话缓存、OCSP、SNI、HTTP/2、HTTP/3 |
| 06 | [URL 重写与请求处理](./06-nginx-rewrite.md) | rewrite、return、if、map、常用重写场景、最佳实践 |
| 07 | [压缩与缓存](./07-nginx-compression-caching.md) | Gzip 压缩、代理缓存、FastCGI 缓存、静态文件缓存 |
| 08 | [日志与监控](./08-nginx-logging-monitoring.md) | 访问日志、错误日志、日志格式、条件日志、stub_status、日志分析 |
| 09 | [安全与访问控制](./09-nginx-security.md) | IP 访问控制、基础认证、请求限制、安全头部、防盗链、WAF |
| 10 | [TCP/UDP Stream 模块](./10-nginx-stream-tcp-udp.md) | TCP/UDP 代理、负载均衡、SSL、PROXY 协议、速率限制 |
| 11 | [邮件代理模块](./11-nginx-mail-proxy.md) | IMAP/POP3/SMTP 代理、认证配置、SSL/TLS、认证服务器 |
| 12 | [性能优化](./12-nginx-performance-tuning.md) | Worker 配置、事件优化、连接复用、缓冲配置、内核参数 |
| 13 | [Git Commit 规范](./13-git-commit-guide.md) | Conventional Commits 格式、类型定义、范围划分、示例库 |
| 14 | [gRPC/uWSGI/SCGI 代理](./14-nginx-grpc-uwsgi.md) | gRPC 代理、uWSGI Python 部署、SCGI 通用代理、模块对比 |
| 15 | [高级特性](./15-nginx-advanced-features.md) | 内部重定向、错误处理、请求拦截、高级路由 |
| 16 | [内部重定向](./16-nginx-internal-redirect.md) | internal 指令、X-Accel-Redirect、try_files、命名 location |
| 17 | [镜像与切片](./17-nginx-mirror-slice.md) | mirror 请求镜像、slice 大文件切片、流量复制 |
| 18 | [Memcached 集成](./18-nginx-memcached.md) | memcached_pass、缓存加速、键值设计 |
| 19 | [HTTP 功能模块详解](./19-nginx-http-modules-detail.md) | access/auth/autoindex/geo/map/realip/referer/secure_link/ssi/sub 等 16 个模块 |
| 20 | [限流与连接控制](./20-nginx-rate-limiting.md) | limit_req 请求限流、limit_conn 连接限制、令牌桶算法、DDoS 防护 |
| 21 | [HTTP/2 与 HTTP/3](./21-nginx-http2-http3.md) | HTTP/2 多路复用、HTTP/3 QUIC、配置迁移、性能对比 |
| 22 | [第三方扩展模块](./22-nginx-third-party-modules.md) | NJS/Lua/Brotli/Cache Purge/Headers More/RTMP/Sticky 模块 |
| 23 | [特殊功能模块](./23-nginx-special-modules.md) | WebDAV/图像过滤/FLV/MP4/HLS 流媒体/XSLT 转换 |
| 24 | [核心与事件模块](./24-nginx-core-events.md) | worker_processes/events/epoll/kqueue/连接数计算 |
| 25 | [内置变量速查表](./25-nginx-variables-reference.md) | HTTP/Stream/SSL/Upstream 变量完整列表150+个) |
| 26 | [Lua 模块深度指南](./26-nginx-lua-guide.md) | OpenResty、ngx_lua 指令、共享字典、cosocket API |
| 27 | [安全深度指南](./27-nginx-security-deep-dive.md) | WAF/ModSecurity、DDoS 防护、OWASP Top 10、安全头部 |
| 28 | [API 网关配置](./28-nginx-api-gateway.md) | API 路由设计、JWT 验证、限流配额、版本控制 |
| 29 | [动态配置与服务发现](./29-nginx-dynamic-config.md) | 动态 upstream、etcd/Consul、dyups、nginx-unit |
| 30 | [njs JavaScript 模块](./30-nginx-njs-guide.md) | njs 引擎、js_import/js_set/js_content、Fetch API、共享字典 |
| 31 | [OpenTelemetry 可观测性](./31-nginx-observability.md) | ngx_otel_module、分布式追踪、Jaeger/Zipkin 集成、采样策略 |
| 32 | [ACME 自动证书管理](./32-nginx-acme-ssl.md) | ngx_http_acme_module、Let's Encrypt、自动续期、HTTP-01/DNS-01 挑战 |
---
## 模块分类索引
### 核心模块
- [核心与事件模块](./24-nginx-core-events.md) - ngx_core_module, ngx_events_module
- [HTTP 核心模块](./03-nginx-http-core.md) - ngx_http_core_module
### 代理与负载均衡
- [反向代理与负载均衡](./04-nginx-proxy-loadbalancing.md) - ngx_http_proxy_module, ngx_http_upstream_module
- [gRPC/uWSGI/SCGI 代理](./14-nginx-grpc-uwsgi.md) - gRPC, Python, CGI 代理
- [TCP/UDP Stream 模块](./10-nginx-stream-tcp-udp.md) - ngx_stream_* 模块
- [邮件代理模块](./11-nginx-mail-proxy.md) - ngx_mail_* 模块
### 安全与访问控制
- [安全与访问控制](./09-nginx-security.md) - 综合安全配置
- [HTTP 功能模块详解](./19-nginx-http-modules-detail.md) - access/auth_basic/auth_request/referer/secure_link
- [限流与连接控制](./20-nginx-rate-limiting.md) - limit_req, limit_conn
### 性能与优化
- [性能优化](./12-nginx-performance-tuning.md) - Worker/事件/缓冲/内核参数
- [压缩与缓存](./07-nginx-compression-caching.md) - Gzip, proxy_cache, fastcgi_cache
- [HTTP/2 与 HTTP/3](./21-nginx-http2-http3.md) - HTTP/2, HTTP/3 QUIC
### 内容处理
- [URL 重写与请求处理](./06-nginx-rewrite.md) - rewrite, return, map
- [镜像与切片](./17-nginx-mirror-slice.md) - mirror, slice
- [特殊功能模块](./23-nginx-special-modules.md) - WebDAV, 图像过滤, 流媒体
### 扩展与第三方
- [第三方扩展模块](./22-nginx-third-party-modules.md) - NJS, Lua, Brotli, RTMP 等
- [Lua 模块深度指南](./26-nginx-lua-guide.md) - OpenResty、ngx_lua、cosocket
- [njs JavaScript 模块](./30-nginx-njs-guide.md) - njs 引擎、JavaScript 扩展
### 安全深度
- [安全与访问控制](./09-nginx-security.md) - 综合安全配置
- [安全深度指南](./27-nginx-security-deep-dive.md) - WAF、DDoS、OWASP
- [ACME 自动证书管理](./32-nginx-acme-ssl.md) - Let's Encrypt、自动 SSL 证书
### 可观测性
- [日志与监控](./08-nginx-logging-monitoring.md) - 访问日志、错误日志、stub_status
- [OpenTelemetry 可观测性](./31-nginx-observability.md) - 分布式追踪、Jaeger/Zipkin
### API 与动态配置
- [API 网关配置](./28-nginx-api-gateway.md) - API 路由、JWT、限流配额
- [动态配置与服务发现](./29-nginx-dynamic-config.md) - 动态 upstream、etcd/Consul
### 参考手册
- [内置变量速查表](./25-nginx-variables-reference.md) - 150+ 个变量完整列表
## 快速参考
### 核心配置结构
```nginx
# 全局配置
worker_processes auto;
error_log /var/log/nginx/error.log;
events {
worker_connections 10240;
}
http {
# HTTP 全局配置
include mime.types;
default_type application/octet-stream;
# 上游服务器
upstream backend {
server 192.168.1.1:8080;
server 192.168.1.2:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
}
}
}
stream {
# TCP/UDP 代理
server {
listen 3306;
proxy_pass mysql:3306;
}
}
mail {
# 邮件代理
server {
listen 25;
protocol smtp;
}
}
```
### 常用命令
```bash
# 测试配置
nginx -t
# 重载配置
nginx -s reload
# 优雅停止
nginx -s quit
# 查看版本和编译参数
nginx -V
```
### 性能优化要点
1. **Worker 进程**`worker_processes auto`
2. **连接数**`worker_connections 10240`
3. **文件传输**`sendfile on`
4. **长连接**`keepalive_timeout 65`
5. **压缩**`gzip on; gzip_comp_level 6`
6. **缓存**`open_file_cache` + `proxy_cache`
7. **SSL 优化**`ssl_session_cache shared:SSL:10m`
### 安全配置要点
1. **隐藏版本**`server_tokens off`
2. **安全协议**`ssl_protocols TLSv1.2 TLSv1.3`
3. **安全头部**HSTS、X-Frame-Options、CSP
4. **请求限制**`limit_req` + `limit_conn`
5. **访问控制**IP 白名单 + 基础认证
---
## 官方资源
- **官方网站**https://nginx.org/
- **官方文档**https://nginx.org/en/docs/
- **模块参考**https://nginx.org/en/docs/ngx_core_module.html
- **FAQ**https://nginx.org/en/docs/faq.html
- **Wiki**https://wiki.nginx.org/
---
## 版本说明
- 文档基于 NGINX 官方文档整理
- 涵盖 NGINX 开源版主要功能
- 部分高级功能需要 NGINX Plus 商业版
- 建议使用 NGINX 1.24+ 版本以获得最新特性

View File

@ -1,511 +0,0 @@
# Lolly 配置文件完整字段清单
> 本文档列出所有支持的配置字段及其枚举值,与代码定义完全同步。
## 顶层配置
| 字段 | 类型 | 说明 |
|------|------|------|
| `server` | ServerConfig | 单服务器模式配置 |
| `servers` | []ServerConfig | 多虚拟主机模式配置 |
| `stream` | []StreamConfig | TCP/UDP Stream 代理配置 |
| `http3` | HTTP3Config | HTTP/3 (QUIC) 配置 |
| `logging` | LoggingConfig | 日志配置 |
| `performance` | PerformanceConfig | 性能配置 |
| `monitoring` | MonitoringConfig | 监控配置 |
---
## ServerConfig 服务器配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `listen` | string | `:8080` | 监听地址,格式 `host:port``:port` |
| `name` | string | `localhost` | 服务器名称,虚拟主机匹配 |
| `static` | []StaticConfig | - | 静态文件服务配置 |
| `proxy` | []ProxyConfig | - | 反向代理规则列表 |
| `ssl` | SSLConfig | - | SSL/TLS 配置 |
| `security` | SecurityConfig | - | 安全配置 |
| `rewrite` | []RewriteRule | - | URL 重写规则 |
| `compression` | CompressionConfig | - | 响应压缩配置 |
| `read_timeout` | duration | `30s` | 读取超时 |
| `write_timeout` | duration | `30s` | 写入超时 |
| `idle_timeout` | duration | `120s` | 空闲超时 |
| `max_conns_per_ip` | int | `1000` | 每 IP 最大连接数 |
| `max_requests_per_conn` | int | `10000` | 每连接最大请求数 |
| `client_max_body_size` | string | `1MB` | **请求体大小限制** |
### client_max_body_size 格式
支持以下格式:
- 纯数字:表示字节数,如 `1048576`
- 带单位:`1b`, `10kb`, `5mb`, `1gb`
- 默认值:`1MB`
---
## StaticConfig 静态文件配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `path` | string | `/` | 匹配路径前缀 |
| `root` | string | - | 静态文件根目录 |
| `index` | []string | `["index.html", "index.htm"]` | 索引文件列表 |
| `try_files` | []string | - | **按顺序尝试查找的文件,支持 SPA** |
| `try_files_pass` | bool | `false` | **内部重定向是否触发中间件** |
### try_files 示例
```yaml
static:
- path: "/"
root: "/var/www/html"
index: ["index.html"]
try_files: ["$uri", "$uri/", "/index.html"] # SPA 部署
try_files_pass: false # 内部重定向不触发中间件
```
**占位符支持:**
- `$uri` - 原始请求 URI
- `$uri/` - URI 作为目录
---
## ProxyConfig 反向代理配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `path` | string | **必填** | 匹配路径前缀 |
| `targets` | []ProxyTarget | **必填** | 后端目标列表 |
| `load_balance` | string | `round_robin` | 负载均衡算法 |
| `hash_key` | string | - | 一致性哈希键 |
| `virtual_nodes` | int | `150` | 一致性哈希虚拟节点数 |
| `health_check` | HealthCheckConfig | - | 健康检查配置 |
| `timeout` | ProxyTimeout | - | 超时配置 |
| `headers` | ProxyHeaders | - | 请求/响应头修改 |
| `cache` | ProxyCacheConfig | - | 代理缓存配置 |
| `client_max_body_size` | string | - | **请求体大小限制(覆盖全局)** |
| `next_upstream` | NextUpstreamConfig | - | 故障转移配置 |
### load_balance 枚举值
| 值 | 说明 |
|-----|------|
| `round_robin` | 轮询(默认) |
| `weighted_round_robin` | 加权轮询 |
| `least_conn` | 最少连接 |
| `ip_hash` | IP 哈希 |
| `consistent_hash` | 一致性哈希 |
### hash_key 枚举值
| 值 | 说明 |
|-----|------|
| `ip` | 客户端 IP |
| `uri` | 请求 URI |
| `header:X-Name` | 指定请求头的值 |
---
## SSLConfig SSL/TLS 配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `cert` | string | - | 证书文件路径PEM 格式) |
| `key` | string | - | 私钥文件路径PEM 格式) |
| `cert_chain` | string | - | 证书链文件路径 |
| `protocols` | []string | `["TLSv1.2", "TLSv1.3"]` | TLS 版本 |
| `ciphers` | []string | - | 加密套件(仅 TLS 1.2 有效) |
| `ocsp_stapling` | bool | `false` | OCSP Stapling |
| `hsts` | HSTSConfig | - | HSTS 配置 |
### protocols 枚举值
| 值 | 说明 |
|-----|------|
| `TLSv1.2` | TLS 1.2 |
| `TLSv1.3` | TLS 1.3(推荐) |
**注意**:不支持 `TLSv1.0``TLSv1.1`(已废弃)
### ciphers 安全要求
拒绝包含以下关键字的不安全套件:
- `RC4`
- `DES`
- `3DES`
- `CBC`
推荐套件:
```
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES256-GCM-SHA384
ECDHE-ECDSA-CHACHA20-POLY1305
ECDHE-RSA-CHACHA20-POLY1305
```
---
## HSTSConfig HSTS 配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `max_age` | int | `31536000` | 过期时间1年 |
| `include_sub_domains` | bool | `true` | 包含子域名 |
| `preload` | bool | `false` | 加入 HSTS 预加载列表 |
---
## SecurityConfig 安全配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `access` | AccessConfig | - | IP 访问控制 |
| `rate_limit` | RateLimitConfig | - | 速率限制 |
| `auth` | AuthConfig | - | 认证配置 |
| `headers` | SecurityHeaders | - | 安全头部 |
| `error_page` | ErrorPageConfig | - | **自定义错误页面** |
---
## ErrorPageConfig 错误页面配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `pages` | map[int]string | - | 状态码到错误页面映射 |
| `default` | string | - | 默认错误页面 |
| `response_code` | int | `0` | 响应状态码覆盖 |
### 示例
```yaml
security:
error_page:
pages:
404: "/var/www/errors/404.html"
500: "/var/www/errors/500.html"
502: "/var/www/errors/502.html"
503: "/var/www/errors/503.html"
default: "/var/www/errors/error.html"
response_code: 200 # 可选:所有错误返回 200
```
---
## AccessConfig 访问控制配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `allow` | []string | `[]` | 允许的 IP/CIDR 列表 |
| `deny` | []string | `[]` | 拒绝的 IP/CIDR 列表 |
| `default` | string | `allow` | 默认动作 |
| `trusted_proxies` | []string | `[]` | 可信代理 CIDR |
### default 枚举值
| 值 | 说明 |
|-----|------|
| `allow` | 默认允许 |
| `deny` | 默认拒绝 |
---
## RateLimitConfig 速率限制配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `request_rate` | int | `0` | 每秒请求数限制0=不限制) |
| `burst` | int | `0` | 突发上限 |
| `conn_limit` | int | `0` | 连接数限制 |
| `key` | string | `ip` | 限流 key 来源 |
| `algorithm` | string | `token_bucket` | 限流算法 |
| `sliding_window_mode` | string | `approximate` | 滑动窗口模式 |
| `sliding_window` | int | `60` | 滑动窗口大小(秒) |
### key 枚举值
| 值 | 说明 |
|-----|------|
| `ip` | 客户端 IP |
| `header` | 请求头值 |
### algorithm 枚举值
| 值 | 说明 |
|-----|------|
| `token_bucket` | 令牌桶(默认) |
| `sliding_window` | 滑动窗口 |
### sliding_window_mode 枚举值
| 值 | 说明 |
|-----|------|
| `approximate` | 近似模式(高性能) |
| `precise` | 精确模式(高精度) |
---
## AuthConfig 认证配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `type` | string | - | 认证类型(空=禁用) |
| `require_tls` | bool | `true` | 强制 HTTPS |
| `algorithm` | string | `bcrypt` | 密码哈希算法 |
| `users` | []User | - | 用户列表 |
| `realm` | string | `Restricted Area` | 认证域 |
| `min_password_length` | int | `8` | 密码最小长度 |
### type 枚举值
| 值 | 说明 |
|-----|------|
| ` ` (空) | 禁用认证 |
| `basic` | HTTP Basic 认证 |
### algorithm 枚举值
| 值 | 说明 |
|-----|------|
| `bcrypt` | bcrypt 算法(默认) |
| `argon2id` | Argon2id 算法 |
---
## SecurityHeaders 安全头部配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `x_frame_options` | string | `DENY` | 防止点击劫持 |
| `x_content_type_options` | string | `nosniff` | 防止 MIME 嗅探 |
| `content_security_policy` | string | - | CSP 策略 |
| `referrer_policy` | string | `strict-origin-when-cross-origin` | 引用策略 |
| `permissions_policy` | string | - | 权限策略 |
### x_frame_options 枚举值
| 值 | 说明 |
|-----|------|
| ` ` (空) | 禁用 |
| `DENY` | 完全禁止嵌入 |
| `SAMEORIGIN` | 仅允许同源嵌入 |
### referrer_policy 枚举值
| 值 | 说明 |
|-----|------|
| `no-referrer` | 不发送 Referer |
| `no-referrer-when-downgrade` | 降级时不发送 |
| `origin` | 仅发送源 |
| `origin-when-cross-origin` | 跨域时仅发送源 |
| `same-origin` | 同源时发送完整 |
| `strict-origin` | 严格模式仅发送源 |
| `strict-origin-when-cross-origin` | 默认值 |
| `unsafe-url` | 始终发送完整 URL |
---
## RewriteRule URL 重写规则
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `pattern` | string | **必填** | 正则匹配模式 |
| `replacement` | string | **必填** | 替换目标 |
| `flag` | string | - | 标志 |
### flag 枚举值
| 值 | 说明 |
|-----|------|
| ` ` (空) | 继续匹配后续规则 |
| `last` | 停止匹配后续规则 |
| `break` | 停止匹配但继续处理 |
| `redirect` | 302 临时重定向 |
| `permanent` | 301 永久重定向 |
---
## CompressionConfig 压缩配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `type` | string | `gzip` | 压缩类型 |
| `level` | int | `6` | 压缩级别0-9 |
| `min_size` | int | `1024` | 最小压缩大小(字节) |
| `types` | []string | 见下方 | 可压缩的 MIME 类型 |
| `gzip_static` | bool | `false` | 启用预压缩文件支持 |
| `gzip_static_extensions` | []string | `[".br", ".gz"]` | 预压缩文件扩展名 |
### type 枚举值
| 值 | 说明 |
|-----|------|
| ` ` (空) | 禁用压缩 |
| `gzip` | 仅 gzip |
| `brotli` | 仅 brotli |
| `both` | gzip + brotli |
### 默认可压缩 MIME 类型
```yaml
types:
- "text/html"
- "text/css"
- "text/javascript"
- "application/json"
- "application/javascript"
```
---
## NextUpstreamConfig 故障转移配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `tries` | int | `1` | 最大尝试次数 |
| `http_codes` | []int | - | 触发重试的 HTTP 状态码 |
**注意**`tries=1` 表示禁用故障转移
### 示例
```yaml
next_upstream:
tries: 3
http_codes: [502, 503, 504]
```
---
## StreamConfig TCP/UDP Stream 代理
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `listen` | string | **必填** | 监听地址 |
| `protocol` | string | **必填** | 协议类型 |
| `upstream` | StreamUpstream | - | 上游配置 |
### protocol 枚举值
| 值 | 说明 |
|-----|------|
| `tcp` | TCP 协议 |
| `udp` | UDP 协议 |
---
## HTTP3Config HTTP/3 配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `enabled` | bool | `false` | 是否启用 |
| `listen` | string | `:443` | UDP 监听地址 |
| `max_streams` | int | `100` | 最大并发流 |
| `idle_timeout` | duration | `60s` | 空闲超时 |
| `enable_0rtt` | bool | `false` | 启用 0-RTT |
---
## LoggingConfig 日志配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `format` | string | `text` | 全局日志格式 |
| `access` | AccessLogConfig | - | 访问日志配置 |
| `error` | ErrorLogConfig | - | 错误日志配置 |
### format 枚举值
| 值 | 说明 |
|-----|------|
| `text` | 文本格式(默认) |
| `json` | JSON 格式 |
### access.format 支持变量
| 变量 | 说明 |
|------|------|
| `$remote_addr` | 客户端 IP |
| `$remote_user` | 认证用户名 |
| `$request` | 请求行 |
| `$status` | 响应状态码 |
| `$body_bytes_sent` | 响应体大小 |
| `$request_time` | 请求处理时间 |
| `$http_referer` | Referer 头 |
| `$http_user_agent` | User-Agent 头 |
| `$time` | 时间戳RFC3339 |
### error.level 枚举值
| 值 | 说明 |
|-----|------|
| `debug` | 调试级别 |
| `info` | 信息级别(默认) |
| `warn` | 警告级别 |
| `error` | 错误级别 |
---
## PerformanceConfig 性能配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `goroutine_pool` | GoroutinePoolConfig | - | Goroutine 池配置 |
| `file_cache` | FileCacheConfig | - | 文件缓存配置 |
| `transport` | TransportConfig | - | HTTP Transport 配置 |
### GoroutinePoolConfig
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `enabled` | bool | `false` | 是否启用 |
| `max_workers` | int | `1000` | 最大 worker 数 |
| `min_workers` | int | `10` | 最小 worker 数(预热) |
| `idle_timeout` | duration | `60s` | 空闲超时 |
### FileCacheConfig
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `max_entries` | int64 | `10000` | 最大缓存条目数 |
| `max_size` | int64 | `268435456` (256MB) | 内存上限 |
| `inactive` | duration | `20s` | 未访问淘汰时间 |
### TransportConfig
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `max_idle_conns` | int | `100` | 最大空闲连接数 |
| `max_idle_conns_per_host` | int | `32` | 每主机空闲连接 |
| `idle_conn_timeout` | duration | `90s` | 空闲超时 |
| `max_conns_per_host` | int | `0` | 每主机最大连接0=不限制) |
---
## MonitoringConfig 监控配置
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `status` | StatusConfig | - | 状态端点配置 |
| `pprof` | PprofConfig | - | pprof 配置 |
### StatusConfig
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `path` | string | `/_status` | 端点路径 |
| `allow` | []string | `["127.0.0.1"]` | 允许访问的 IP |
### PprofConfig
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `enabled` | bool | `false` | 是否启用 |
| `path` | string | `/debug/pprof` | 端点路径前缀 |
| `allow` | []string | `["127.0.0.1"]` | 允许访问的 IP |
---
## 变更记录
- 2026-04-07: 首次创建,发现 `--generate-config` 缺少 `client_max_body_size``try_files``try_files_pass``error_page` 字段

View File

@ -1,288 +0,0 @@
# NGINX 文档完善建议
基于对 nginx.org 官方文档的深度分析,对比 docs/ 目录下现有的 25 个文档,识别出以下可完善的部分。
---
## 一、缺失或需要新增的文档
### 1. NGINX Lua 模块深度指南
**优先级:高**
现有 `22-nginx-third-party-modules.md` 对 NJS/Lua 有简要介绍,但 Lua 模块功能强大,值得独立文档。
**建议内容:**
- OpenResty 环境搭建
- ngx_lua 核心指令content_by_lua、access_by_lua、rewrite_by_lua
- Lua 共享字典ngx.shared.DICT
- cosocket API非阻塞网络 I/O
- 与 Redis/MySQL 集成
- 性能优化技巧
### 2. NGINX 作为 API 网关
**优先级:中**
现代架构中 nginx 常作为 API 网关使用。
**建议内容:**
- API 路由设计模式
- 请求/响应转换
- JWT 验证(通过 Lua 或 NJS
- 限流与配额管理
- API 版本控制策略
- OpenAPI/Swagger 集成
### 3. NGINX 动态配置
**优先级:中**
现代部署需要动态配置能力。
**建议内容:**
- 动态 upstreamnginx-plus 或开源方案)
- 使用 etcd/Consul 进行服务发现
- dyups 模块使用
- nginx-unit 简介
- 动态 SSL 证书加载
### 4. NGINX 安全最佳实践(增强版)
**优先级:高**
现有 `09-nginx-security.md` 内容良好,可扩展:
**建议补充:**
- Bot 检测与防护
- WAF 配置深度指南ModSecurity
- DDoS 防护策略
- OWASP Top 10 防护
- 安全响应头完整配置
- CVE 历史漏洞与修复版本
---
## 二、现有文档可扩展的内容
### 15-nginx-advanced-features.md
**当前:** 仅 94 行,内容相对简略
**建议扩展:**
1. **调试与诊断**
- debug 日志级别
- debug_points 指令
- worker_debug_connection
2. **错误处理**
- error_page 高级用法
- 自定义错误页面
- try_files 与 error_page 配合
3. **请求拦截**
- post_action 指令
- 日志记录后操作
### 16-nginx-internal-redirect.md
**当前:** 119 行,内容较好
**建议扩展:**
1. **SSI服务端包含详解**
- SSI 指令列表
- 虚拟包含 vs 文件包含
- 条件执行
2. **命名 location 深度解析**
- 命名 location 语法
- 与 error_page 配合
- 重定向链追踪
### 17-nginx-mirror-slice.md
**当前:** 143 行,内容较好
**建议扩展:**
1. **高级镜像场景**
- 条件镜像(基于请求头、路径)
- 镜像流量采样
- 镜像目标选择策略
2. **slice 与缓存高级配置**
- 多级缓存配置
- 缓存预热策略
- 缓存失效策略
### 19-nginx-http-modules-detail.md
**当前:** 约 1200 行,内容丰富
**建议补充:**
1. **ngx_http_addition_module**
- before/after 内容追加
- 与 SSI 配合使用
2. **ngx_http_sub_module 高级用法**
- 多规则替换
- 正则替换
- 变量替换
---
## 三、新增文档建议清单
| 序号 | 文档名称 | 优先级 | 预计行数 |
|------|----------|--------|----------|
| 26 | nginx-lua-guide.md | 高 | 800+ |
| 27 | nginx-api-gateway.md | 中 | 600+ |
| 28 | nginx-dynamic-config.md | 中 | 500+ |
| 29 | nginx-security-deep-dive.md | 高 | 700+ |
| 30 | nginx-troubleshooting.md | 中 | 400+ |
| 31 | nginx-observability.md | 中 | 500+ |
---
## 四、文档质量建议
### 统一格式
- 所有文档使用统一的标题层级
- 配置示例添加语法高亮标记
- 表格格式统一
### 交叉引用
- 添加相关文档链接
- 引用官方文档链接
### 版本标注
- 功能版本要求标注
- 已废弃功能标注
---
## 五、NGINX HTTP 模块深度分析Agent 分析结果)
### 5.1 HTTP 模块完整列表(按类别)
#### 核心模块
| 模块名称 | 功能描述 | 依赖关系 |
|----------|----------|----------|
| `ngx_http_core_module` | HTTP 核心功能 | 无(必需) |
| `ngx_http_log_module` | 访问日志记录 | core |
| `ngx_http_upstream_module` | 上游服务器负载均衡 | core |
#### 请求处理与路由模块
| 模块名称 | 功能描述 | 依赖关系 |
|----------|----------|----------|
| `ngx_http_rewrite_module` | URI 重写(支持正则) | core |
| `ngx_http_proxy_module` | 反向代理 | upstream |
| `ngx_http_fastcgi_module` | FastCGI 协议代理 | upstream |
| `ngx_http_uwsgi_module` | uWSGI 协议代理 | upstream |
| `ngx_http_scgi_module` | SCGI 协议代理 | upstream |
#### 安全与访问控制模块
| 模块名称 | 功能描述 | 依赖关系 |
|----------|----------|----------|
| `ngx_http_access_module` | IP 访问控制 | core |
| `ngx_http_auth_basic_module` | HTTP 基本认证 | core |
| `ngx_http_auth_request_module` | 子请求认证 | proxy |
| `ngx_http_ssl_module` | SSL/TLS 支持 | core |
| `ngx_http_limit_req_module` | 请求速率限制 | core |
| `ngx_http_limit_conn_module` | 连接数限制 | core |
| `ngx_http_realip_module` | 真实 IP 替换 | core |
#### 压缩与优化模块
| 模块名称 | 功能描述 | 依赖关系 |
|----------|----------|----------|
| `ngx_http_gzip_module` | GZIP 压缩 | core |
| `ngx_http_gunzip_module` | GZIP 解压 | gzip |
| `ngx_http_headers_module` | 响应头处理 | core |
#### 上游负载均衡算法模块
| 模块名称 | 功能描述 | 依赖关系 |
|----------|----------|----------|
| `ngx_http_upstream_hash_module` | 一致性哈希负载均衡 | upstream |
| `ngx_http_upstream_ip_hash_module` | IP 哈希负载均衡 | upstream |
| `ngx_http_upstream_least_conn_module` | 最少连接负载均衡 | upstream |
| `ngx_http_upstream_keepalive_module` | 上游 keepalive 连接 | upstream |
### 5.2 最常用的 15 个指令
| 排名 | 指令 | 用途 |
|------|------|------|
| 1 | `listen` | 端口监听 |
| 2 | `server_name` | 虚拟主机 |
| 3 | `location` | 路由匹配 |
| 4 | `root` | 根目录 |
| 5 | `proxy_pass` | 代理目标 |
| 6 | `try_files` | 文件尝试 |
| 7 | `rewrite` | URL 重写 |
| 8 | `return` | 返回响应 |
| 9 | `index` | 索引文件 |
| 10 | `error_page` | 错误页面 |
| 11 | `client_max_body_size` | 上传限制 |
| 12 | `keepalive_timeout` | 连接保持 |
| 13 | `gzip` | 压缩开关 |
| 14 | `ssl_certificate` | SSL 证书 |
| 15 | `access_log` | 访问日志 |
---
## 六、NGINX Stream 模块深度分析Agent 分析结果)
### 6.1 Stream 核心模块指令
| 指令 | 语法 | 上下文 | 说明 |
|------|------|--------|------|
| `stream` | `stream { ... }` | main | 定义 stream 配置块 |
| `server` | `server { ... }` | stream | 定义虚拟服务器 |
| `listen` | `listen address:port [options]` | server | 监听端口配置 |
| `preread_buffer_size` | `preread_buffer_size size` | stream, server | 预读取缓冲区大小 |
| `preread_timeout` | `preread_timeout timeout` | stream, server | 预读取超时时间 |
### 6.2 Stream 子模块完整列表
| 模块名称 | 功能描述 |
|----------|----------|
| **核心模块** | |
| ngx_stream_core_module | Stream 核心功能 |
| **代理模块** | |
| ngx_stream_proxy_module | TCP/UDP 代理转发 |
| ngx_stream_ssl_module | SSL/TLS 支持 |
| ngx_stream_ssl_preread_module | SSL 预读取SNI 路由) |
| **上游模块** | |
| ngx_stream_upstream_module | 上游服务器管理 |
| ngx_stream_hash_module | 一致性哈希负载均衡 |
| ngx_stream_least_conn_module | 最少连接负载均衡 |
| ngx_stream_random_module | 随机负载均衡 |
| **访问控制** | |
| ngx_stream_access_module | 允许/拒绝访问控制 |
| ngx_stream_limit_conn_module | 连接数限制 |
| ngx_stream_geo_module | 基于 IP 的地理位置变量 |
| ngx_stream_geoip_module | GeoIP 数据库支持 |
| **日志与监控** | |
| ngx_stream_log_module | 日志记录 |
| ngx_stream_return_module | 返回指定值并关闭连接 |
### 6.3 现有文档 10-nginx-stream-tcp-udp.md 对比
现有文档已覆盖:
- ✅ TCP/UDP 代理基础配置
- ✅ 负载均衡算法
- ✅ SSL 终止
- ✅ PROXY 协议
- ✅ 速率限制
建议补充:
- 🔧 健康检查详细配置active health check
- 🔧 stream 日志格式自定义
- 🔧 UDP 响应数配置proxy_responses
- 🔧 SSL preread 模块SNI 路由)
---
## 七、与 Lolly 项目的关系
基于 docs/plan.mdLolly 是一个类似 nginx 的 Go 实现。文档完善有助于:
1. **功能对齐**:识别 nginx 功能,作为 Lolly 开发参考
2. **配置翻译**nginx 配置 → YAML 配置设计参考
3. **测试用例**:文档中的配置示例可作为测试用例
建议在 Lolly 开发过程中,同步更新 nginx 文档作为功能对照。
---
*生成时间2026-04-03*

View File

@ -1,389 +0,0 @@
# nginx 模块文档大纲
## 1. ngx_http_grpc_module (gRPC 代理)
### 1.1 模块概述
ngx_http_grpc_module 模块用于将请求代理到 gRPC 服务器。该模块自 nginx 1.13.10 版本开始提供,支持 HTTP/2 协议上的 gRPC 通信。
**主要特性:**
- 支持 gRPC-over-HTTP/2 协议
- 支持 gRPC 服务端流、客户端流和双向流
- 支持 SSL/TLS 加密连接
- 支持负载均衡和健康检查
- 支持请求头自定义和超时配置
### 1.2 核心指令表格
| 指令 | 语法 | 说明 | 默认值 | 上下文 |
|------|------|------|--------|--------|
| grpc_pass | `grpc_pass address;` | 设置 gRPC 服务器地址 | - | location, if in location |
| grpc_set_header | `grpc_set_header field value;` | 设置传递给 gRPC 服务器的请求头 | - | http, server, location |
| grpc_hide_header | `grpc_hide_header field;` | 隐藏 gRPC 响应中的指定头字段 | - | http, server, location |
| grpc_pass_header | `grpc_pass_header field;` | 允许传递隐藏的头字段 | - | http, server, location |
| grpc_buffer_size | `grpc_buffer_size size;` | 设置读取响应的缓冲区大小 | 4k/8k | http, server, location |
| grpc_buffering | `grpc_buffering on/off;` | 启用/禁用响应缓冲 | on | http, server, location |
| grpc_connect_timeout | `grpc_connect_timeout time;` | 设置与服务器建立连接的超时 | 60s | http, server, location |
| grpc_send_timeout | `grpc_send_timeout time;` | 设置向服务器发送请求的超时 | 60s | http, server, location |
| grpc_read_timeout | `grpc_read_timeout time;` | 设置读取服务器响应的超时 | 60s | http, server, location |
| grpc_socket_keepalive | `grpc_socket_keepalive on/off;` | 启用 TCP keepalive | off | http, server, location |
| grpc_ssl_certificate | `grpc_ssl_certificate file;` | 指定客户端 SSL 证书 | - | http, server |
| grpc_ssl_certificate_key | `grpc_ssl_certificate_key file;` | 指定客户端 SSL 证书密钥 | - | http, server |
| grpc_ssl_ciphers | `grpc_ssl_ciphers ciphers;` | 指定 SSL 加密算法 | DEFAULT | http, server |
| grpc_ssl_protocols | `grpc_ssl_protocols protocols;` | 指定 SSL 协议版本 | TLSv1 TLSv1.1 TLSv1.2 | http, server |
| grpc_ssl_verify | `grpc_ssl_verify on/off;` | 启用服务器证书验证 | off | http, server |
### 1.3 配置示例
**基本配置:**
```nginx
location / {
grpc_pass grpc://localhost:50051;
}
```
**带 SSL 的配置:**
```nginx
location / {
grpc_pass grpcs://grpc.example.com:443;
grpc_ssl_certificate /etc/nginx/client.crt;
grpc_ssl_certificate_key /etc/nginx/client.key;
grpc_ssl_verify on;
grpc_ssl_trusted_certificate /etc/nginx/ca.crt;
}
```
**自定义请求头:**
```nginx
location / {
grpc_set_header Host $host;
grpc_set_header X-Real-IP $remote_addr;
grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
grpc_set_header X-Forwarded-Proto $scheme;
grpc_pass grpc://localhost:50051;
}
```
**超时和缓冲配置:**
```nginx
location / {
grpc_pass grpc://localhost:50051;
grpc_connect_timeout 5s;
grpc_send_timeout 10s;
grpc_read_timeout 30s;
grpc_buffering off;
grpc_buffer_size 16k;
}
```
**负载均衡配置:**
```nginx
upstream grpc_backend {
server 127.0.0.1:50051 weight=5;
server 127.0.0.1:50052;
server 127.0.0.1:50053 backup;
keepalive 32;
}
location / {
grpc_pass grpc://grpc_backend;
grpc_socket_keepalive on;
}
```
### 1.4 与普通 HTTP 代理的区别
| 特性 | gRPC 代理 | 普通 HTTP 代理 (proxy_pass) |
|------|-----------|------------------------------|
| 协议 | HTTP/2 必须 | HTTP/1.0, HTTP/1.1, HTTP/2 |
| 传输 | 二进制协议 (Protocol Buffers) | 文本协议 |
| 流支持 | 原生支持双向流 | 需要特殊配置 (chunked) |
| 连接复用 | 多路复用 (Multiplexing) | 连接池 |
| 头部传递 | 使用 grpc_set_header | 使用 proxy_set_header |
| 错误处理 | gRPC 状态码 | HTTP 状态码 |
### 1.5 应用场景
1. **微服务架构网关**:作为 gRPC 服务的统一入口
2. **SSL/TLS 终止**:对外提供 HTTPS内部使用明文 gRPC
3. **负载均衡**:在多个 gRPC 服务实例间分发请求
4. **A/B 测试**:基于路由规则将流量导向不同版本的服务
5. **流量监控**:收集 gRPC 调用指标和日志
---
## 2. ngx_http_uwsgi_module (uWSGI 代理)
### 2.1 模块概述
ngx_http_uwsgi_module 模块用于将请求代理到 uwsgi 协议服务器,主要用于 Python WSGI 应用程序(如 Django、Flask的部署。
**主要特性:**
- 专为 Python WSGI 应用设计
- 使用 uwsgi 协议(比 HTTP/FastCGI 更高效)
- 支持 Unix 域套接字和 TCP 套接字
- 支持参数传递和环境变量设置
- 支持缓冲、缓存和超时配置
### 2.2 核心指令表格
| 指令 | 语法 | 说明 | 默认值 | 上下文 |
|------|------|------|--------|--------|
| uwsgi_pass | `uwsgi_pass address;` | 设置 uwsgi 服务器地址 | - | location, if in location |
| uwsgi_param | `uwsgi_param parameter value [if_not_empty];` | 设置传递给 uWSGI 的参数 | - | http, server, location |
| uwsgi_modifier1 | `uwsgi_modifier1 number;` | 设置 uWSGI 数据包修饰符1 | 0 | http, server, location |
| uwsgi_modifier2 | `uwsgi_modifier2 number;` | 设置 uWSGI 数据包修饰符2 | 0 | http, server, location |
| uwsgi_bind | `uwsgi_bind address [transparent];` | 绑定到特定地址 | - | http, server, location |
| uwsgi_buffering | `uwsgi_buffering on/off;` | 启用/禁用响应缓冲 | on | http, server, location |
| uwsgi_buffer_size | `uwsgi_buffer_size size;` | 设置响应缓冲区大小 | 4k/8k | http, server, location |
| uwsgi_buffers | `uwsgi_buffers number size;` | 设置缓冲区数量和大小 | 8 4k/8k | http, server, location |
| uwsgi_busy_buffers_size | `uwsgi_busy_buffers_size size;` | 设置忙缓冲区大小 | 8k/16k | http, server, location |
| uwsgi_cache | `uwsgi_cache zone;` | 启用响应缓存 | - | http, server, location |
| uwsgi_cache_key | `uwsgi_cache_key string;` | 设置缓存键 | - | http, server, location |
| uwsgi_cache_valid | `uwsgi_cache_valid time;` | 设置缓存有效期 | - | http, server, location |
| uwsgi_connect_timeout | `uwsgi_connect_timeout time;` | 连接超时 | 60s | http, server, location |
| uwsgi_send_timeout | `uwsgi_send_timeout time;` | 发送超时 | 60s | http, server, location |
| uwsgi_read_timeout | `uwsgi_read_timeout time;` | 读取超时 | 60s | http, server, location |
| uwsgi_hide_header | `uwsgi_hide_header field;` | 隐藏响应头 | - | http, server, location |
| uwsgi_pass_header | `uwsgi_pass_header field;` | 允许传递隐藏头 | - | http, server, location |
| uwsgi_ignore_client_abort | `uwsgi_ignore_client_abort on/off;` | 忽略客户端中止 | off | http, server, location |
| uwsgi_intercept_errors | `uwsgi_intercept_errors on/off;` | 拦截错误码 | off | http, server, location |
### 2.3 配置示例
**基本 Django/Flask 配置:**
```nginx
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/app.sock;
}
```
**TCP 套接字配置:**
```nginx
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
```
**自定义参数:**
```nginx
location / {
uwsgi_param SCRIPT_NAME /myapp;
uwsgi_param PATH_INFO $1;
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
uwsgi_pass unix:/run/uwsgi/app.sock;
}
```
**带缓存的配置:**
```nginx
http {
uwsgi_cache_path /var/cache/nginx/uwsgi levels=1:2 keys_zone=uwsgi:10m max_size=1g;
server {
location / {
uwsgi_cache uwsgi;
uwsgi_cache_key $host$request_uri;
uwsgi_cache_valid 200 10m;
uwsgi_cache_valid 404 1m;
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/app.sock;
}
}
}
```
**负载均衡配置:**
```nginx
upstream uwsgi_backend {
server 127.0.0.1:3031 weight=5;
server 127.0.0.1:3032;
server 127.0.0.1:3033 backup;
keepalive 32;
}
location / {
include uwsgi_params;
uwsgi_pass uwsgi_backend;
}
```
### 2.4 与 FastCGI 的对比
| 特性 | uWSGI | FastCGI |
|------|-------|---------|
| 设计目标 | Python WSGI 专用 | 通用语言接口 |
| 协议开销 | 更低(二进制协议) | 较高(文本协议)|
| 性能 | 更高 | 较低 |
| 配置复杂度 | 简单 | 较复杂 |
| 语言支持 | 主要为 Python | PHP、Perl、Ruby 等 |
| 功能扩展 | 丰富的插件系统 | 有限 |
| 进程管理 | 内置多种模式 | 依赖外部管理器 |
### 2.5 应用场景
1. **Django 应用部署**:高性能 Python Web 框架部署
2. **Flask 应用部署**:轻量级 Python Web 框架部署
3. **Python API 服务**RESTful API 后端服务
4. **机器学习模型服务**:部署 ML 模型推理服务
5. **数据科学应用**Jupyter、Streamlit 等应用托管
---
## 3. ngx_http_scgi_module (SCGI 代理)
### 3.1 模块概述
ngx_http_scgi_module 模块用于将请求代理到 SCGISimple Common Gateway Interface服务器。SCGI 是一种简化版的 CGI 协议,旨在提供比传统 CGI 更好的性能。
**主要特性:**
- 简化的 CGI 协议实现
- 比传统 CGI 更快的性能(保持持久连接)
- 支持 Unix 域套接字和 TCP 套接字
- 支持参数传递和环境变量设置
- 轻量级,适合小型项目
### 3.2 核心指令表格
| 指令 | 语法 | 说明 | 默认值 | 上下文 |
|------|------|------|--------|--------|
| scgi_pass | `scgi_pass address;` | 设置 SCGI 服务器地址 | - | location, if in location |
| scgi_param | `scgi_param parameter value [if_not_empty];` | 设置传递给 SCGI 的参数 | - | http, server, location |
| scgi_bind | `scgi_bind address [transparent];` | 绑定到特定地址 | - | http, server, location |
| scgi_buffering | `scgi_buffering on/off;` | 启用/禁用响应缓冲 | on | http, server, location |
| scgi_buffer_size | `scgi_buffer_size size;` | 设置响应缓冲区大小 | 4k/8k | http, server, location |
| scgi_buffers | `scgi_buffers number size;` | 设置缓冲区数量和大小 | 8 4k/8k | http, server, location |
| scgi_busy_buffers_size | `scgi_busy_buffers_size size;` | 设置忙缓冲区大小 | 8k/16k | http, server, location |
| scgi_cache | `scgi_cache zone;` | 启用响应缓存 | - | http, server, location |
| scgi_cache_key | `scgi_cache_key string;` | 设置缓存键 | - | http, server, location |
| scgi_cache_valid | `scgi_cache_valid time;` | 设置缓存有效期 | - | http, server, location |
| scgi_connect_timeout | `scgi_connect_timeout time;` | 连接超时 | 60s | http, server, location |
| scgi_send_timeout | `scgi_send_timeout time;` | 发送超时 | 60s | http, server, location |
| scgi_read_timeout | `scgi_read_timeout time;` | 读取超时 | 60s | http, server, location |
| scgi_hide_header | `scgi_hide_header field;` | 隐藏响应头 | - | http, server, location |
| scgi_pass_header | `scgi_pass_header field;` | 允许传递隐藏头 | - | http, server, location |
| scgi_ignore_client_abort | `scgi_ignore_client_abort on/off;` | 忽略客户端中止 | off | http, server, location |
| scgi_intercept_errors | `scgi_intercept_errors on/off;` | 拦截错误码 | off | http, server, location |
| scgi_pass_request_headers | `scgi_pass_request_headers on/off;` | 传递请求头 | on | http, server, location |
| scgi_pass_request_body | `scgi_pass_request_body on/off;` | 传递请求体 | on | http, server, location |
### 3.3 配置示例
**基本配置:**
```nginx
location / {
include scgi_params;
scgi_pass localhost:4000;
}
```
**Unix 域套接字配置:**
```nginx
location / {
include scgi_params;
scgi_pass unix:/tmp/scgi.sock;
}
```
**自定义参数:**
```nginx
location / {
scgi_param SCRIPT_NAME /myapp;
scgi_param PATH_INFO $1;
scgi_param QUERY_STRING $query_string;
scgi_param REQUEST_METHOD $request_method;
scgi_param CONTENT_TYPE $content_type;
scgi_param CONTENT_LENGTH $content_length;
scgi_param REQUEST_URI $request_uri;
scgi_param DOCUMENT_ROOT $document_root;
scgi_param SERVER_PROTOCOL $server_protocol;
scgi_param REMOTE_ADDR $remote_addr;
scgi_param REMOTE_PORT $remote_port;
scgi_param SERVER_PORT $server_port;
scgi_param SERVER_NAME $server_name;
scgi_pass localhost:4000;
}
```
**带缓存的配置:**
```nginx
http {
scgi_cache_path /var/cache/nginx/scgi levels=1:2 keys_zone=scgi:10m max_size=1g;
server {
location / {
scgi_cache scgi;
scgi_cache_key $host$request_uri;
scgi_cache_valid 200 5m;
include scgi_params;
scgi_pass localhost:4000;
}
}
}
```
### 3.4 与 FastCGI/uWSGI 的对比
| 特性 | SCGI | FastCGI | uWSGI |
|------|------|---------|-------|
| 协议复杂度 | 简单(纯文本)| 中等 | 复杂(二进制)|
| 性能 | 中等 | 中等 | 最高 |
| 资源占用 | 低 | 中等 | 中等 |
| 连接方式 | 持久连接 | 持久连接 | 持久连接 |
| 功能丰富度 | 基础功能 | 较丰富 | 最丰富 |
| 语言支持 | 通用 | PHP 为主 | Python 为主 |
| 实现难度 | 简单 | 中等 | 复杂 |
| 适用场景 | 小型项目 | 中型项目 | 大型生产环境 |
**SCGI vs FastCGI**
- **SCGI**:协议更简单,实现更容易,适合轻量级应用
- **FastCGI**更成熟生态更好PHP 应用首选
**SCGI vs uWSGI**
- **SCGI**:通用协议,不绑定特定语言
- **uWSGI**Python 专用,功能最丰富
### 3.5 应用场景
1. **小型 CGI 应用**:简单脚本语言的 Web 接口
2. **自定义语言运行环境**:为特定语言实现 SCGI 接口
3. **嵌入式系统**:资源受限环境下的轻量级方案
4. **原型开发**:快速验证 Web 应用概念
5. **学习目的**:理解 CGI 协议的简化实现
---
## 4. 三个模块对比总结
| 维度 | ngx_http_grpc_module | ngx_http_uwsgi_module | ngx_http_scgi_module |
|------|---------------------|----------------------|---------------------|
| **协议类型** | HTTP/2 (gRPC) | uwsgi (二进制) | SCGI (文本) |
| **主要语言** | 多语言 | Python | 多语言 |
| **性能** | 高HTTP/2 多路复用)| 很高 | 中等 |
| **典型应用** | 微服务通信 | Django/Flask | CGI 脚本 |
| **配置复杂度** | 中等 | 简单 | 简单 |
| **功能特性** | 流、SSL、LB | 缓冲、缓存 | 基础代理 |
| **生态成熟度** | 快速增长 | 成熟Python| 小众 |
## 5. 选择建议
- **微服务/API 网关**:选择 **grpc_module**
- **Python Web 应用**:选择 **uwsgi_module**
- **简单 CGI/轻量级**:选择 **scgi_module** 或 **fastcgi_module**
---
*文档生成时间2026-04-03*

File diff suppressed because it is too large Load Diff

View File

@ -1,322 +0,0 @@
# Lolly 项目需求文档
> 本文档用于指导 AI 实现一个高性能 HTTP 服务器项目。
## 一、项目定位
创建一个类似 nginx 的高性能 HTTP 服务器,项目名称为 **lolly**
### 核心目标
- 实现与 nginx 相同的核心功能,但**不是 1:1 复刻**
- 比 nginx 更加现代、更加易用
- 充分利用 Go 语言特性goroutine、channel、标准库等
- 保持高性能(高并发、低内存消耗)
### 技术约束
| 约束项 | 要求 |
|--------|------|
| 语言 | 纯 Go 实现,不依赖 C 库 |
| 部署 | 静态链接,单二进制文件即可运行 |
| 配置 | YAML 格式(比 nginx 的 nginx.conf 更简单易用) |
| 兼容性 | 跨平台支持Linux、macOS、Windows |
---
## 二、功能需求
### 必须实现的核心功能(参考 docs/ 目录详细文档)
#### 2.1 HTTP 核心模块(优先级:最高)
参考:`docs/03-nginx-http-core.md`
- **Server/Location 配置**:虚拟主机、请求路由
- **静态文件服务**:高效 serving 静态资源
- **请求处理**:请求头、请求体、超时控制
- **keep-alive**:长连接支持
- **MIME 类型**:自动识别内容类型
#### 2.2 反向代理与负载均衡(优先级:高)
参考:`docs/04-nginx-proxy-loadbalancing.md`
- **反向代理**proxy_pass 功能
- **负载均衡算法**轮询、权重、最少连接、IP 哈希
- **健康检查**:主动/被动健康检测
- **WebSocket 支持**Upgrade 协议处理
- **代理缓存**:响应缓存机制
#### 2.3 SSL/TLS 与 HTTPS优先级
参考:`docs/05-nginx-ssl-https.md`
- **HTTPS 服务**:证书配置
- **SSL 会话缓存**:减少握手开销
- **HTTP/2 支持**:现代化协议
- **自动证书管理**:可选 ACME/Let's Encrypt 集成
#### 2.4 URL 重写与请求处理(优先级:中)
参考:`docs/06-nginx-rewrite.md`
- **URL 重写**:灵活的路由规则
- **重定向**301/302 等状态码
- **请求修改**:添加/修改请求头
#### 2.5 压缩与缓存(优先级:中)
参考:`docs/07-nginx-compression-caching.md`
- **Gzip/Brotli 压缩**:响应压缩
- **静态文件缓存**:文件描述符缓存
- **代理响应缓存**:缓存代理请求结果
#### 2.6 日志与监控(优先级:中)
参考:`docs/08-nginx-logging-monitoring.md`
- **访问日志**:可定制日志格式
- **错误日志**:分级日志
- **状态监控**:实时状态端点(类似 stub_status
- **日志轮转**:内置支持
#### 2.7 安全与访问控制(优先级:高)
参考:`docs/09-nginx-security.md`
- **IP 访问控制**:白名单/黑名单
- **请求限制**:速率限制、连接限制
- **安全头部**:自动添加安全相关 HTTP 头
- **基础认证**Basic Auth 支持
#### 2.8 TCP/UDP Stream 代理(优先级:低)
参考:`docs/10-nginx-stream-tcp-udp.md`
- **TCP 代理**:四层代理支持
- **UDP 代理**UDP 流处理
- **Stream 负载均衡**:四层负载均衡
#### 2.9 邮件代理(优先级:最低,可选)
参考:`docs/11-nginx-mail-proxy.md`
- IMAP/POP3/SMTP 代理(可后期实现或不实现)
---
## 三、配置文件设计
### 设计原则
配置文件采用 YAML 格式,设计原则:
1. **简洁易用**:比 nginx.conf 更直观
2. **结构清晰**:层级明确,易于理解
3. **类型安全**:配置值有明确类型
4. **默认合理**:开箱即用,最小配置即可运行
### 配置文件示例(仅供参考)
```yaml
# lolly.yaml - 最简配置示例
server:
listen: 8080
name: example.com
# 静态文件服务
static:
root: /var/www/html
index: [index.html, index.htm]
# 反向代理
proxy:
- path: /api
target: http://backend:8080
load_balance: round_robin
# SSL/HTTPS
ssl:
cert: /etc/ssl/cert.pem
key: /etc/ssl/key.pem
# 日志配置
logging:
access: /var/log/lolly/access.log
error: /var/log/lolly/error.log
level: info
# 性能配置
performance:
workers: auto # 自动匹配 CPU 核心数
max_connections: 10000
keepalive_timeout: 60s
```
### 配置与 nginx 的对比
| nginx 配置 | lolly 配置(预期) |
|------------|-------------------|
| `worker_processes auto;` | `performance.workers: auto` |
| `listen 80;` | `server.listen: 80` |
| `root /var/www;` | `server.static.root: /var/www` |
| `proxy_pass http://backend;` | `server.proxy.target: http://backend` |
| `ssl_certificate cert.pem;` | `server.ssl.cert: cert.pem` |
---
## 四、架构设计要求
### 4.1 Go 特性利用
充分利用 Go 语言特性:
- **Goroutine**:每个连接一个 goroutine天然高并发
- **Channel**:进程内通信、信号处理
- **标准库**`net/http``net``io``sync`
- **Context**:请求超时控制、取消传播
- **接口**:模块化设计,接口抽象
### 4.2 模块化设计
```
lolly/
├── cmd/
│ └── lolly/
│ └── main.go # 入口
├── internal/
│ ├── config/ # 配置解析
│ ├── server/ # HTTP 服务器核心
│ ├── proxy/ # 反向代理
│ ├── loadbalance/ # 负载均衡算法
│ ├── ssl/ # SSL/TLS 支持
│ ├── rewrite/ # URL 重写
│ ├── cache/ # 缓存模块
│ ├── compression/ # 压缩模块
│ ├── logging/ # 日志模块
│ ├── security/ # 安全模块
│ ├── stream/ # TCP/UDP 代理
│ └── middleware/ # 中间件系统
├── pkg/
│ └── utils/ # 公共工具
└── configs/
└── lolly.yaml # 默认配置
```
### 4.3 性能要求
- 单机支持数万并发连接
- 低内存消耗(相比 nginx 同等功能)
- 静态文件高效传输(利用 `sendfile` 等优化)
- 连接复用、缓冲池
---
## 五、开发规范
### 5.1 提交规范
每完成一个功能点提交一次,提交格式遵循最佳实践:
```
<type>(<scope>): <subject>
<body>
<footer>
```
**type 类型**
- `feat`: 新功能
- `fix`: 修复 bug
- `docs`: 文档变更
- `refactor`: 重构
- `test`: 测试相关
- `chore`: 构建/工具相关
**示例**
```
feat(server): 实现静态文件服务功能
- 支持配置根目录和索引文件
- 实现 MIME 类型自动识别
- 支持 keep-alive 长连接
Closes #1
```
### 5.2 代码注释规范
严格遵循 `docs/comments.md` 规范:
- **中文注释**:所有注释使用中文
- **文件头注释**:每个文件必须有文件头注释
- **函数注释**:说明用途、参数、返回值、注意事项
- **说明"为什么"**:注释解释原因,而非仅描述代码
- **TODO/FIXME**:使用标准格式,标注负责人和时间
### 5.3 实现顺序建议
建议按以下顺序逐步实现:
1. **第一阶段**:项目骨架 + 配置解析
- 项目目录结构
- YAML 配置解析
- 基本命令行参数
2. **第二阶段**HTTP 核心功能
- 基础 HTTP 服务器
- Server/Location 配置
- 静态文件服务
3. **第三阶段**:代理与负载均衡
- 反向代理
- 负载均衡算法
- 健康检查
4. **第四阶段**:安全与 SSL
- SSL/TLS 支持
- IP 访问控制
- 请求限制
5. **第五阶段**:增强功能
- URL 重写
- 压缩
- 缓存
- 日志系统
6. **第六阶段**:高级功能
- TCP/UDP Stream
- 性能优化
- 监控端点
---
## 六、参考资料
| 资料位置 | 内容 |
|----------|------|
| `docs/01-nginx-overview.md` | nginx 架构、信号控制、命令行参数 |
| `docs/02-nginx-installation.md` | 安装构建(参考模块结构) |
| `docs/03-nginx-http-core.md` | HTTP 核心模块详细功能 |
| `docs/04-nginx-proxy-loadbalancing.md` | 反向代理、负载均衡详细功能 |
| `docs/05-nginx-ssl-https.md` | SSL/HTTPS 配置详细功能 |
| `docs/06-nginx-rewrite.md` | URL 重写详细功能 |
| `docs/07-nginx-compression-caching.md` | 压缩、缓存详细功能 |
| `docs/08-nginx-logging-monitoring.md` | 日志、监控详细功能 |
| `docs/09-nginx-security.md` | 安全功能详细说明 |
| `docs/10-nginx-stream-tcp-udp.md` | TCP/UDP 代理功能 |
| `docs/11-nginx-mail-proxy.md` | 邮件代理(可选参考) |
| `docs/12-nginx-performance-tuning.md` | 性能优化参考 |
| `docs/comments.md` | Go 代码注释规范(必须遵循) |
---
## 七、其他说明
- 本项目是学习/实验性质的高性能服务器实现
- 优先保证代码质量和可维护性
- 功能完整性优先于极致性能优化
- 遇到设计决策问题时,选择"更简单易用"的方案