lolly/docs/config/security/access-control.conf
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

138 lines
3.5 KiB
Plaintext
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.

# ============================================================
# Nginx IP 访问控制配置示例
# ============================================================
#
# 功能说明:
# - IP/CIDR 白名单和黑名单
# - 基于地理位置的访问控制
# - 可信代理配置
#
# Lolly 对应配置:
# server:
# security:
# access:
# allow: ["10.0.0.0/8", "192.168.0.0/16"]
# deny: ["192.168.100.0/24"]
# default: "allow" # 默认策略allow 或 deny
# trusted_proxies: ["10.0.0.0/8"] # 可信代理 CIDR
# ============================================================
server {
listen 80;
server_name access.example.com;
# 访问控制配置
# Lolly 对应: security.access 配置块
# 白名单模式(只允许特定 IP
location /admin {
allow 10.0.0.0/8; # 内网
allow 192.168.0.0/16; # VPN
allow 127.0.0.1; # 本地
deny all; # 拒绝其他
proxy_pass http://admin-backend:8080;
}
# 黑名单模式(拒绝特定 IP
location /api {
deny 192.168.100.0/24; # 拒绝特定子网
deny 10.0.50.50; # 拒绝特定 IP
allow all; # 允许其他
proxy_pass http://api-backend:8080;
}
# 混合模式(白名单 + 黑名单)
location /sensitive {
allow 10.0.0.0/8;
deny 10.0.50.0/24; # 在白名单内排除
deny all;
proxy_pass http://backend:8080;
}
# 访问控制错误页面
error_page 403 = @access_denied;
location @access_denied {
default_type application/json;
return 403 '{"error": "Access denied"}';
}
}
# 基于地理位置的访问控制(需要 GeoIP 模块)
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
# 国家黑名单
map $geoip_country_code $allowed_country {
default yes;
CN no; # 拒绝中国(示例)
RU no; # 拒绝俄罗斯(示例)
}
server {
listen 80;
server_name geo.example.com;
if ($allowed_country = no) {
return 403;
}
location / {
proxy_pass http://backend:8080;
}
}
}
# 可信代理配置(用于 X-Forwarded-For 解析)
http {
# 设置可信代理
# Lolly 对应: security.access.trusted_proxies
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 192.168.0.0/16;
set_real_ip_from 127.0.0.1;
# 使用哪个 Header 获取真实 IP
real_ip_header X-Forwarded-For;
# 或使用 X-Real-IP
# real_ip_header X-Real-IP;
# 递归搜索(从最后一个可信代理往前找)
real_ip_recursive on;
server {
listen 80;
server_name proxy.example.com;
location /api {
# $remote_addr 现在是真实客户端 IP
proxy_pass http://backend:8080;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
# IP 访问控制说明:
#
# 1. 规则顺序:
# - 按配置顺序匹配
# - 首次匹配生效
# - allow/deny 可混合使用
#
# 2. CIDR 格式:
# - 192.168.1.0/24 表示 192.168.1.0-255
# - 10.0.0.0/8 表示整个 10.x.x.x 段
# - 127.0.0.1 单个 IP
#
# 3. 可信代理:
# - 在负载均衡器后需要配置
# - 从 X-Forwarded-For 提取真实 IP
# - 防止 IP 伪造攻击
#
# 4. Lolly 配置:
# - allow: 白名单列表
# - deny: 黑名单列表
# - default: 默认策略
# - trusted_proxies: 可信代理 CIDR