fix(nginx): admin 用 upstream+proxy_pass 去前缀,替代变量/rewrite
Made-with: Cursor
This commit is contained in:
@@ -15,5 +15,5 @@
|
||||
2. **确认 dist 完整**:`deploy/admin/dist/assets/` 下须有与 `index.html` 中 `<script type="module">` 引用**同名**的哈希文件;发版后应**整目录**替换 `dist`(勿只拷 `index.html`)。
|
||||
3. **本地重建**:在项目根按 `pull-and-restart.sh` 方式在 `admin/` 执行 `npm run build`,`vite.config` 中 `base` 须为 `'/admin/'`。
|
||||
4. **勿用旧版 `nginx/admin.conf`**:若曾把仅含 `location /` 的旧配置拷到服务器,会导致 `/assets/*.js` 全部变成 `index.html`(约 640B、MIME 错)。请以 **`deploy/admin/default.conf`** 或 **`admin/nginx.conf`** 为准,并 **`docker compose restart admin nginx`**。
|
||||
5. **变量 `proxy_pass` 与路径**:外层 `yh_nginx` 对 **`location /admin/`** 须 **`rewrite ^/admin/(.*)$ /$1 break`** 再 **`proxy_pass http://$upstream_admin:80`**(无 URI 后缀)。否则 Nginx 会把 **`/admin/assets/...` 原样**传到 admin,内层只认 **`/assets/...`** → 全部回退 `index.html`(网络面板里 JS/CSS 与 document 同几百字节)。**`/api/`** 不要用 rewrite(API 需要完整 `/api/...`)。
|
||||
5. **外层 `/admin/` 反代**:`yuheng.docker.conf.tpl` 使用 **`upstream yh_admin_upstream { server admin:80; }`** + **`location /admin/ { proxy_pass http://yh_admin_upstream/; }`**(尾斜杠),由 Nginx **标准规则**去掉 `/admin` 前缀;勿对 admin 使用 **变量** `proxy_pass`(会把完整 `/admin/...` 传到上游 → 内层无法匹配 `/assets/` → 白屏)。另含 **`location = /admin { return 301 /admin/; }`**,避免无尾斜杠误走前台。
|
||||
6. **`restart.sh` 构建 admin** 已与 **`pull-and-restart.sh` 一致**(挂载**项目根**到容器,否则 `@yh-web` 无法解析)。发版后脚本会执行 **`scripts/verify-admin-dist.sh`**:若 `index.html` 引用的 chunk 在 `dist/assets/` 中缺失或过小(几百字节),会直接报错退出,避免白屏上线。
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
# 由 scripts/nginx-entrypoint-wait-dns.sh 在启动时 sed 替换 @@NGINX_RESOLVER@@(来自容器 /etc/resolv.conf)
|
||||
# 再写入 /etc/nginx/conf.d/default.conf。使用 resolver + 变量 proxy_pass,避免 Podman 下启动瞬间 host not found in upstream。
|
||||
# 再写入 /etc/nginx/conf.d/default.conf。web/api 仍用变量 proxy_pass + resolver(Podman 下动态解析)。
|
||||
# admin 使用 upstream + proxy_pass …/ 可正确去掉 /admin 前缀;勿用变量 proxy_pass,否则会把 /admin/assets/… 原样传到上游 → 白屏。
|
||||
|
||||
upstream yh_admin_upstream {
|
||||
server admin:80;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl;
|
||||
@@ -8,7 +13,6 @@ server {
|
||||
server_name yuheng.yuxindazhineng.com;
|
||||
client_max_body_size 800m;
|
||||
|
||||
# valid 过短会频繁重解析,宿主机 DNS 往往解析不了 compose 服务名 → 502;与 entrypoint 中优先 127.0.0.11 配合
|
||||
resolver @@NGINX_RESOLVER@@ valid=300s ipv6=off;
|
||||
|
||||
ssl_certificate /etc/ssl/yh_web/yuheng.yuxindazhineng.com/fullchain.pem;
|
||||
@@ -18,7 +22,6 @@ server {
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
|
||||
|
||||
# 验证文件在 443 上直接读挂载目录,不经 yh_web 反代(避免正则 alias 403、与内网路径不一致)
|
||||
location ~ ^/[A-Za-z0-9._-]+\.(txt|html|xml)$ {
|
||||
root /verify-root;
|
||||
try_files $uri =404;
|
||||
@@ -26,27 +29,9 @@ server {
|
||||
add_header Cache-Control "no-store";
|
||||
}
|
||||
|
||||
# 变量 proxy_pass 不会按普通规则去掉 location 前缀,会把完整 URI /admin/assets/... 传到上游 → 内层无法匹配 /assets/ → 全站 index.html 白屏
|
||||
# 须先 rewrite 去掉 /admin,再反代(与 /api/ 不同:api 需要保留 /api 前缀)
|
||||
location / {
|
||||
set $upstream_web web;
|
||||
proxy_pass http://$upstream_web:80;
|
||||
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;
|
||||
}
|
||||
|
||||
location /admin/ {
|
||||
rewrite ^/admin/(.*)$ /$1 break;
|
||||
set $upstream_admin admin;
|
||||
proxy_pass http://$upstream_admin:80;
|
||||
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;
|
||||
# 无尾斜杠会落到 location / → 误走 web
|
||||
location = /admin {
|
||||
return 301 /admin/;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
@@ -58,4 +43,24 @@ server {
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
# 尾斜杠形式:proxy_pass 带 / 会去掉 /admin 前缀,上游收到 /assets/…、/index.html 等
|
||||
location /admin/ {
|
||||
proxy_pass http://yh_admin_upstream/;
|
||||
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;
|
||||
}
|
||||
|
||||
location / {
|
||||
set $upstream_web web;
|
||||
proxy_pass http://$upstream_web:80;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user