Files
ywsandClaude Opus 5.5 7c5eec6049 正式环境:爱发电收款全自动(精确反查 + 定时对账 + 买家自查),修复付款区拿不到专链;支付宝预留待开通;维护页开关
- 修线上 bug:付款配置真实键名是 afdian / afdian_uid,后端只认 payplatform* → 页面拿不到专链,
  付款区只显示「付款方式找金钻要」。现在两种键名都认、都返回(含价格档位)。
- 回调反查先按 out_trade_no 精确查,查不到再翻页;去掉「金额唯一匹配」猜单。
- 新增定时对账(每分钟,有待付款单才调接口):回调丢了也自动激活;与回调走同一套处理(账本去重 / 金额闸门 / 激活)。
- 买家点「我已经付好了」先当场对账,付过就直接排队;没付照旧登记待确认。
- 付款信息带 alipay:{on:false, note:待开通},下单页显示灰色「支付宝扫码 · 待开通」,不接任何通道。
- 一起上线 c8f3ae4(进下单页闪黑修复)。
- 新测试《收款对账测试》14 条;回执复现 33、数据库 38、内容后台 86 全过。
- nginx 维护页开关写进部署文档(只拦页面,接口 / 回调 / 同域应用放行)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 09:25:27 +00:00

189 lines
9.1 KiB
Markdown
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 反代。这一份是跟 `/order/` 相关的路由与限流,
> 给你看**路由是怎么接的、限额是怎么设的**。值已按脱敏规则替换,**照形状看,别照字面抄**。
---
## 一、限流区(定义在 nginx 的 http 层)
```nginx
# 下单相关:一般接口 90 次/分钟,提交接口单独收紧到 10 次/分钟
limit_req_zone $binary_remote_addr zone=<ZONE_ORDER>:1m rate=90r/m;
limit_req_zone $binary_remote_addr zone=<ZONE_SUBMIT>:1m rate=10r/m;
```
**为什么给提交接口单独一条**:它是唯一会**收上传**的入口,也是最值得刷的入口。
别的接口刷快一点无所谓(就是查状态),提交接口必须压住。
---
## 二、`/order/api/` 的路由
```nginx
# ① 提交接口:单独限流 + 单独放宽请求体
location = /order/api/submit {
limit_req zone=<ZONE_SUBMIT> burst=5 nodelay;
limit_req_status 429;
client_max_body_size 4m; # 皮肤是 base64 进来的
proxy_pass http://localhost:<PORT>/api/submit;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; # 后端要拿它做「单 IP 限单」
proxy_read_timeout 120s;
add_header Cache-Control "no-store" always;
}
# ② 回传接口:**单独一条**,因为成品一张 1~3MB(base64 后更大)
# ⚠️ nginx 的 `location =` 精确匹配**优先于**前缀匹配 ——
# 想只给这一个路径放宽体积,不能靠下面那条前缀规则
location = /order/api/finish {
limit_req zone=<ZONE_ORDER> burst=40 nodelay;
limit_req_status 429;
client_max_body_size 16m;
proxy_pass http://localhost:<PORT>/api/finish;
# ...
}
# ③ 其余接口
location /order/api/ {
limit_req zone=<ZONE_ORDER> burst=40 nodelay;
limit_req_status 429;
proxy_pass http://localhost:<PORT>/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# ...
}
```
### ④ 内容后台(2026-10-05 加)
```nginx
# 登录 / 内容接口。⚠️ 必须带 X-Forwarded-Proto —— 后端看它判断是不是 https,不带一律拒绝(内容后台只走 https)
location /order/api/cms/ {
limit_req zone=<ZONE_ORDER> burst=20 nodelay;
limit_req_status 429;
client_max_body_size 16m; # 画展传图:单张 10MB,base64 后约 13.4MB
proxy_pass http://localhost:<PORT>/api/cms/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 后台发布出来的内容文件:文件名不变、内容会变 → 让浏览器每次都问一下有没有新的
location /content/ { add_header Cache-Control "no-cache"; }
location /order/content/ { add_header Cache-Control "no-cache"; }
# 页面本身(2026-10-06 起门户是多页:index / gallery / tools / universe / links / about.html)不缓存:改版后手机浏览器别老吃旧副本
location ~ \.html$ { add_header Cache-Control "no-cache"; }
# 站点固定信息(群号 / 备案号 / 邮箱):正式服务器上的这份是真实值,部署别用仓库里的占位覆盖
location = /assets/site-info.js { add_header Cache-Control "no-cache"; }
# 后台页面本身不缓存(改版后别人拿到旧页面会调错接口)
location /admin/ { add_header Cache-Control "no-store"; }
# 画展上传的图(文件名是随机的,可以长缓存)
location /uploads/ { add_header Cache-Control "public, max-age=2592000"; }
# 整站 http → https,并加 HSTS(登录页绝不能走 http)
server { listen 80; server_name shop.example.com; return 301 https://$host$request_uri; }
# 在 443 的 server 块里加:
# add_header Strict-Transport-Security "max-age=31536000" always;
```
> 没配好证书之前**不要开放内容后台**:后端会拒绝所有非 https 的请求(返回 403「内容后台只能用 https 打开」)。
⚠️ **打包下载(zip)那条要另外配**:`proxy_buffering off` + 更长的 `proxy_read_timeout`,
因为它是**先落临时文件再边读边发**的,不能在 nginx 这层再缓存一份(小机器内存受不住)。
---
## 三、买家那边的路由(订单链接)
```nginx
location ~ "^/order/[A-Za-z0-9]{6,16}$" {
try_files /order/index.html =404;
add_header Cache-Control "no-store" always;
add_header X-Robots-Tag "noindex, nofollow, noarchive" always;
}
# 兜底:/order/ 下面找不到的路径一律给下单页,**绝不给 404**
location /order/ {
try_files $uri $uri/ /order/index.html;
add_header Cache-Control "no-store" always;
add_header X-Robots-Tag "noindex, nofollow, noarchive" always;
}
```
**这两条的用意,请你连做法一起评**:
1. **`^/order/<6~16位>` 这条必须有**。买家拿到的是 `/order/<订单号>?t=<取件码>`,
没有这条路由会掉到主站规则**直接 404** —— 而买家一关页面就**再也找不回订单**。
(页面文案还写着"请记下订单号用于查询下载",那就成了骗人。)
2. **兜底那条绝不能返回 404**。买家从聊天软件复制链接**常夹进不可见字符**
(实测踩到:路径里多了个零宽空格 → 404),看着就像**网站坏了**。
给下单页最差只是"没找到那一单",页面自己会提示。
3. **正则里的 `{6,16}` 必须用引号包住**,否则 nginx 会把花括号当配置块起始,
报 `unknown directive`(实测踩到)。
4. **`no-store` + `noindex`**:订单页带取件码,不该被缓存也不该被搜索引擎收录
(一是降曝光,二是链接本身就是凭证)。
---
## 四、这一层我们没做的事(也算"做法",欢迎质疑)
- **没上 WAF / 没有额外防护层**:这台机器很小,装不下重的东西;
防护全在后端自己的限流 + 两把令牌 + 来源 IP 门上。
- **没做多机/负载均衡**:单机单进程,够用。
- **HTTPS 用自动签发的证书**,续期自动化,没有额外动作。
## ⑤ 安全加固(2026-10-06,线上已加)
```nginx
# 文件:/etc/nginx/snippets/jz-sec-base.conf
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always;
# 文件:/etc/nginx/snippets/jz-sec-csp.conf(只给本站页面)
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; media-src 'self' blob:; font-src 'self' data:; connect-src 'self'; frame-src https://player.bilibili.com; worker-src 'self' blob:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" always;
# server 块里:
include snippets/jz-sec-base.conf;
include snippets/jz-sec-csp.conf;
location ~* (\.bak[^/]*|\.maint[^/]*|\.old[^/]*|\.orig|\.swp|~|\.v[0-9]+-(live|new)[^/]*)$ { return 404; }
location ~* ^/(order/)?preview-[^/]*\.html$ { return 404; }
location ~ /\.(?!well-known/) { return 404; }
location = /content/site.json { return 404; }
location = /order/content/order.json { return 404; }
# 回调单独限流(http 级:limit_req_zone $binary_remote_addr zone=jzhook:1m rate=20r/m;)
location ~ ^/order/api/(payplatform|afdian)-hook$ {
limit_req zone=jzhook burst=10 nodelay; limit_req_status 429; client_max_body_size 64k;
rewrite ^/order/api/(.*)$ /api/$1 break; proxy_pass http://localhost:<PORT>;
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
add_header Cache-Control "no-store" always; include snippets/jz-sec-base.conf;
}
# ⚠️ 每个自己写了 add_header 的 location 里都要再 include 上面两个文件(nginx 不继承 server 级的 add_header)
# nginx.conf 的 http 块:server_tokens off;
```
## ⑥ 维护页开关(2026-10-07 加)
放在 `jinzuan.work` 的 server 块里(两个安全头 include 之后)。**开关不用 reload**:
- 开:`sudo touch /var/www/jinzuan/.maintenance && sudo chown www-data:www-data /var/www/jinzuan/.maintenance`
- 关:`sudo rm -f /var/www/jinzuan/.maintenance`
只拦金钻客栈的页面(返回 503 + `/maintenance.html`);`/order/api/`(爱发电回调、渲染机、内容后台)、同域其它应用、
`/assets/` `/fonts/` 一律放行。带请求头 `X-Jz-Bypass: <BYPASS_SECRET>` 可以绕过维护页验证新页面(密钥只在服务器配置里)。
```nginx
set $jz_maint 0;
if (-f /var/www/jinzuan/.maintenance) { set $jz_maint 1; }
if ($uri ~ "^/(order/api/|api/|ws$|todo|gw|hit$|admin|uploads/|assets/|fonts/|maintenance\.html$|\.well-known/)") { set $jz_maint 0; }
if ($http_x_jz_bypass = "<BYPASS_SECRET>") { set $jz_maint 0; }
if ($jz_maint = 1) { rewrite ^ /__jz_maint last; }
location = /__jz_maint { internal; error_page 503 /maintenance.html; return 503; }
location = /maintenance.html { add_header Cache-Control "no-store" always; add_header Retry-After "900" always;
include snippets/jz-sec-base.conf; include snippets/jz-sec-csp.conf; }
```