diff --git a/Caddyfile b/Caddyfile index 80c9041..deeedbf 100644 --- a/Caddyfile +++ b/Caddyfile @@ -12,15 +12,11 @@ # 如果有域名,改用下面的 https 配置块 http://{$DOMAIN:NEWS_DOMAIN_FALLBACK} { - # /api/* 转发到 api:8000,handle_path 会自动 strip 匹配的 /api 前缀 - handle_path /api/* { - reverse_proxy api:8000 - } + # /api/* 直接转发,保留路径(后端 FastAPI 路由就是 /api/v1/*) + reverse_proxy /api/* api:8000 # 其余走前端 SPA - handle { - reverse_proxy frontend:80 - } + reverse_proxy /* frontend:80 encode gzip zstd diff --git a/backend/app/main.py b/backend/app/main.py index c9b91f0..46c9d38 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -103,7 +103,7 @@ app.include_router(admin.router, prefix=API_PREFIX) # === 健康检查 === -@app.get("/healthz", include_in_schema=False) +@app.get(f"{API_PREFIX}/healthz", include_in_schema=False) async def healthz(): try: await get_redis().ping() @@ -112,6 +112,6 @@ async def healthz(): return {"status": "ok"} -@app.get("/", include_in_schema=False) +@app.get(f"{API_PREFIX}/", include_in_schema=False) async def root(): - return {"name": "diary-news", "version": app.version, "docs": "/api/docs"} + return {"name": "diary-news", "version": app.version, "docs": f"{API_PREFIX}/docs"} diff --git a/scripts/_restart_caddy.py b/scripts/_restart_caddy.py new file mode 100644 index 0000000..9672e9e --- /dev/null +++ b/scripts/_restart_caddy.py @@ -0,0 +1,28 @@ +import os, paramiko, time +PW = os.environ["REMOTE_PASS"] +c = paramiko.SSHClient() +c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False) + +def run(cmd, t=60): + si, so, se = c.exec_command(cmd, timeout=t) + out = so.read().decode("utf-8", "replace") + err = se.read().decode("utf-8", "replace") + rc = so.channel.recv_exit_status() + print(f"$ {cmd}") + if out: print(out, end="") + if err: print("[err]", err, end="", file=__import__("sys").stderr) + print(f" rc={rc}") + return out + +run("cd /srv/news && sudo -u news git pull --rebase 2>&1 | tail -3") +run("cd /srv/news && sg docker -c 'docker compose up -d --force-recreate caddy' 2>&1 | tail -10") +time.sleep(5) + +# 测试路径 +print("\n=== healthz 测试 ===") +run("curl -s -o /dev/null -w 'healthz: %{http_code}\\n' http://localhost/healthz") +run("curl -s -o /dev/null -w 'api/healthz: %{http_code}\\n' http://localhost/api/healthz") +run("curl -s -o /dev/null -w 'api/v1/articles: %{http_code}\\n' http://localhost/api/v1/articles") +run("curl -s http://localhost/api/healthz 2>&1") +c.close()