- plugin-domain 7 文件取上游 ddd4172+(config/store 独立、update_domains 三泛型) - 路由对齐上游:purge 移至 /api/admin/purge;开关指令 sso_domain → ipass_proxy - 未命中语义对齐上游:access 未命中由插件直接 404(不落转发层兜底), e2e zone 外 Host 断言同步修正 - nginx.conf / nginx.test.conf:sso_state/data_dir/api → ipass_*(sso_iap 保留, sso 插件特有开关)
213 lines
9.3 KiB
Nginx Configuration File
213 lines
9.3 KiB
Nginx Configuration File
# ============================================================================
|
||
# SSO 统一认证网关 —— 生产配置模板
|
||
#
|
||
# 两个 server,对应两层能力:
|
||
# sso.<域名> IdP + 管理台:签发令牌、管理账号与接入方
|
||
# *.<域名>(或 *.<子域>)IAP:转发前完成认证并注入 X-Auth-*
|
||
# *dm.<企业域名> 企业域名转发:子域名 → 后端(域名插件,申请/审核后写入 sub: 共享内存)
|
||
#
|
||
# 部署前必须替换:域名、证书路径、上游 proxy_pass 地址、OAUTH_* 环境变量。
|
||
# 本文件**不参与**本机 e2e(那是 conf/nginx.test.conf)。
|
||
#
|
||
# 阶段处理器注册在 http 级,靠 `sso_api` / `sso_iap` 两个开关按 location 收敛:
|
||
# 少了开关,管理台自己也会被 IAP 门禁 401 掉。
|
||
# ============================================================================
|
||
|
||
load_module /usr/local/nginx/modules/libsso_gateway.so;
|
||
|
||
worker_processes auto;
|
||
error_log /usr/local/nginx/logs/error.log info;
|
||
pid /usr/local/nginx/logs/nginx.pid;
|
||
|
||
# nginx 会剥掉所有环境变量,机制类配置必须显式白名单。
|
||
# 未设置时不报错,模块按各自缺省值兜底(见 source/rust/src/config.rs)。
|
||
env ADMIN_INIT_PASSWORD; # 播种管理员口令,仅首次启动用
|
||
env OAUTH_SIGNING_SECRET; # HS256 对称密钥;不设则每次重启令牌全失效
|
||
env OAUTH_ISSUER; # 令牌 iss 与 discovery 的 issuer,必须等于对外访问地址
|
||
env OAUTH_COOKIE_DOMAIN; # 跨子域 SSO 用,如 .example.com
|
||
|
||
events {
|
||
worker_connections 2048;
|
||
}
|
||
|
||
http {
|
||
include mime.types;
|
||
default_type application/json; # 业务侧默认 JSON,HTML 在对应 location 单独覆盖
|
||
|
||
# 转发 location 里的 `proxy_set_header Connection $connection_upgrade` 依赖此变量;
|
||
# 缺它 nginx -t 直接报 unknown "connection_upgrade" variable,服务起不来。
|
||
map $http_upgrade $connection_upgrade {
|
||
default upgrade;
|
||
'' close;
|
||
}
|
||
|
||
# $auth_sub 来自模块注入(access 阶段)。没有它,日志里只能看到「谁被拦了」,
|
||
# 看不到「谁访问了哪个后端」——排查越权时这两个信息缺一不可。
|
||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||
'$status $body_bytes_sent "$http_referer" "$http_user_agent" '
|
||
'"$http_x_forwarded_for" "$upstream_addr" auth_sub=$auth_sub';
|
||
|
||
access_log /usr/local/nginx/logs/access.log main;
|
||
|
||
sendfile on;
|
||
keepalive_timeout 65;
|
||
|
||
# ========== 共享内存(管理会话 / SSO 会话 / 授权码 / 接入方注册表)==========
|
||
# 一张表按前缀共用,容量实测约 124 B/条,10m 足够放约 8 万条。
|
||
ipass_state state 10m;
|
||
ipass_data_dir /usr/local/nginx/data;
|
||
|
||
# 可选 TLS:conf/tls/ 下放任意 *.conf(完整的 443 server 块)即启用对应域名的 HTTPS。
|
||
# 目录为空时 include 空匹配,不影响启动。说明见 conf/tls/README.md
|
||
include /usr/local/nginx/conf/tls/*.conf;
|
||
|
||
# ==================================================================
|
||
# IdP + 管理台
|
||
#
|
||
# 一个 server 同时承载授权端点与管理接口:接入方与终端用户访问的是同一份
|
||
# issuer,分成两个域名会让 discovery 文档里的地址与 Cookie 作用域都不一致。
|
||
# ==================================================================
|
||
server {
|
||
listen 443 ssl;
|
||
http2 on;
|
||
server_name sso.example.com;
|
||
|
||
ssl_certificate /usr/local/nginx/conf/tls/example.com/fullchain.cer;
|
||
ssl_certificate_key /usr/local/nginx/conf/tls/example.com/example.com.key;
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
|
||
client_max_body_size 2m;
|
||
client_body_buffer_size 1m;
|
||
|
||
# 管理台单页(只读静态资源,数据全部走 /api/admin/*)
|
||
location = /admin {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
location = /admin/ {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
location = / {
|
||
return 302 /admin;
|
||
}
|
||
|
||
# 其余全部交给模块分发。**不要**逐条列 location:
|
||
# 除标准端点外还有 MaxKey 别名(/oauth/v20/*、/oauth2.0/*)以及带
|
||
# context-path 的写法(/maxkey/oauth/v20/*,模块内做前缀归一化),
|
||
# 逐条列举必然漏,漏掉的表现是接入方拿到 HTML 404。
|
||
location / { ipass_api on; }
|
||
}
|
||
|
||
# 明文 80 只做跳转,避免用户在浏览器里手敲地址后停在错误页
|
||
server {
|
||
listen 80;
|
||
server_name sso.example.com;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
|
||
# ==================================================================
|
||
# IAP:受保护的业务系统
|
||
#
|
||
# 应用完全不写认证代码,只读 X-Auth-* 头。
|
||
# 后端地址按环境写死(本项目不做动态上游路由——那是流量层网关的职责)。
|
||
# 多后端时按 server/location 拆分即可,不要引入动态变量:
|
||
# 那样会把「谁能在哪台机器上开洞」变成运行时数据。
|
||
# ==================================================================
|
||
server {
|
||
listen 443 ssl;
|
||
http2 on;
|
||
server_name app.example.com;
|
||
|
||
ssl_certificate /usr/local/nginx/conf/tls/example.com/fullchain.cer;
|
||
ssl_certificate_key /usr/local/nginx/conf/tls/example.com/example.com.key;
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
|
||
location / {
|
||
# 门禁:access 阶段判定身份,未登录 → 401 → error_page 跳登录页
|
||
sso_iap on;
|
||
|
||
proxy_pass http://127.0.0.1:8080;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
proxy_set_header X-Forwarded-Proto $scheme;
|
||
proxy_set_header Upgrade $http_upgrade;
|
||
proxy_set_header Connection $connection_upgrade;
|
||
|
||
# ---- 身份注入:网关断言覆盖客户端伪造同名头 ----
|
||
# 后端只信这五个头。它们由模块在 access 阶段写入(见 lib.rs set_auth_vars)。
|
||
proxy_set_header X-Auth-User $auth_user;
|
||
proxy_set_header X-Auth-Email $auth_email;
|
||
proxy_set_header X-Auth-Groups $auth_groups;
|
||
proxy_set_header X-Auth-Sub $auth_sub;
|
||
proxy_set_header X-Auth-Token $auth_token;
|
||
|
||
proxy_connect_timeout 5s;
|
||
proxy_send_timeout 300s;
|
||
proxy_read_timeout 300s;
|
||
}
|
||
|
||
error_page 401 = @oauth_login;
|
||
location @oauth_login {
|
||
return 302 "https://sso.example.com/login?redirect=$scheme://$host$request_uri";
|
||
}
|
||
}
|
||
|
||
# 明文 80 跳转(同 IdP)
|
||
server {
|
||
listen 80;
|
||
server_name app.example.com;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
# ==================================================================
|
||
# 企业域名转发(域名插件 plugin-domain)
|
||
#
|
||
# 用户申请子域名 → 管理员审核放行 → sub:<子域名> 写入共享内存;
|
||
# 请求进来时 access 阶段解析 Host 得到后端地址,proxy_pass 据此转发。
|
||
#
|
||
# ⚠️ 子域名**必须以 `dm` 结尾**(自动生成的形状是 `i<8位数字>dm`):
|
||
# 下面的正则捕获组是 `.+dm`,不以 dm 结尾的域名根本匹配不到本 server。
|
||
# ⚠️ 后端地址是 `host:port`:若填域名而非 IP,需要配 resolver(见下)。
|
||
# ==================================================================
|
||
server {
|
||
listen 443 ssl;
|
||
http2 on;
|
||
server_name ~^(?<subdomain>.+dm)\.mayishangshu\.cn$;
|
||
|
||
ssl_certificate /usr/local/nginx/conf/tls/example.com/fullchain.cer;
|
||
ssl_certificate_key /usr/local/nginx/conf/tls/example.com/example.com.key;
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
|
||
# 后端若写成域名,必须显式指定 DNS;写 IP:PORT 则不需要
|
||
# resolver 223.5.5.5 valid=30s ipv6=off;
|
||
|
||
location / {
|
||
# 转发开关:access 阶段查 sub: 共享内存,命中写入 $proxy_backend_addr,
|
||
# 未命中(不存在 / 待审核 / 已过期)直接 404
|
||
ipass_proxy on;
|
||
|
||
proxy_pass http://$proxy_backend_addr;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
proxy_set_header X-Forwarded-Proto $scheme;
|
||
proxy_set_header Upgrade $http_upgrade;
|
||
proxy_set_header Connection $connection_upgrade;
|
||
|
||
proxy_connect_timeout 5s;
|
||
proxy_send_timeout 300s;
|
||
proxy_read_timeout 300s;
|
||
}
|
||
}
|
||
|
||
# 明文 80 跳转
|
||
server {
|
||
listen 80;
|
||
server_name ~^(?<subdomain>.+dm)\.mayishangshu\.cn$;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
}
|