xfy 6543422281 docs: 添加 Nginx 配置和 Lua 脚本示例文档
- config: 反向代理、缓存、负载均衡、安全、SSL 等配置模板
- lua: API 网关、认证、动态路由、限流、WebSocket 等脚本示例

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 17:59:22 +08:00

72 lines
2.0 KiB
Plaintext
Raw Permalink 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.

# ============================================================
# Nginx 负载均衡 - IP 哈希配置示例
# ============================================================
#
# 功能说明:
# - 同一客户端 IP 始终路由到同一服务器
# - 实现会话保持Session Persistence
# - 适用于无状态服务器但有会话需求的场景
#
# Lolly 对应配置:
# server:
# proxy:
# - path: "/api"
# targets:
# - url: "http://backend1:8080"
# - url: "http://backend2:8080"
# - url: "http://backend3:8080"
# load_balance: "ip_hash"
# ============================================================
upstream backend {
# IP 哈希算法
# Lolly 对应: load_balance: "ip_hash"
ip_hash;
server backend1:8080;
server backend2:8080;
server backend3:8080;
# 注意: backup 参数在 ip_hash 模式下不生效
}
server {
listen 80;
server_name iphash.example.com;
location /api {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
# IP 哈希算法说明:
#
# 1. 工作原理:
# hash_key = hash(client_ip) % server_count
# - 使用客户端 IPv4 地址的前三段进行哈希
# - IPv6 使用整个地址
# - 保证同一 IP 总是路由到同一服务器
#
# 2. 适用场景:
# - 传统 Session 存储(本地内存 Session
# - 无外部 Session 存储Redis/Memcached
# - 本地缓存依赖(文件缓存)
#
# 3. 限制:
# - 服务器增减时哈希重新计算,可能影响所有用户
# - 不适合动态伸缩场景
# - 权重参数不生效
# - backup 不生效
#
# 4. 替代方案:
# - 使用一致哈希consistent_hash
# - 使用外部 Session 存储Redis
# - 使用 sticky cookie 模块
#
# 5. 注意事项:
# - 在负载均衡器后需要获取真实 IP
# - 配合 X-Forwarded-For 头部使用
# - 大量相同 IP代理用户可能导致不平衡