From c83e905c633c5c27f1001d9667c8d12fdf55690a Mon Sep 17 00:00:00 2001 From: puke <1129090915@qq.com> Date: Sat, 6 Dec 2025 13:41:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=8D=E5=90=91=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=8D=8F=E8=AE=AE=E5=A4=B4=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=20Nginx/Traefik=20=E5=90=8E?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E8=AF=86=E5=88=AB=20HTTPS=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api/app.py b/api/app.py index fa402ed..afa9e3b 100644 --- a/api/app.py +++ b/api/app.py @@ -34,7 +34,7 @@ if str(_project_root) not in sys.path: import argparse from contextlib import asynccontextmanager -from fastapi import FastAPI +from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from loguru import logger @@ -108,6 +108,24 @@ app = FastAPI( lifespan=lifespan, ) +# Add middleware for handling reverse proxy headers (Nginx/Traefik) +@app.middleware("http") +async def handle_forwarded_proto(request: Request, call_next): + """ + Handle X-Forwarded-Proto header from reverse proxy + + This ensures that request.base_url returns the correct protocol (https) + when the application is behind a reverse proxy like Nginx or Traefik. + """ + # Check for X-Forwarded-Proto header (standard) + forwarded_proto = request.headers.get("x-forwarded-proto") + if forwarded_proto: + # Update the request scheme + request.scope["scheme"] = forwarded_proto + + response = await call_next(request) + return response + # Add CORS middleware if api_config.cors_enabled: app.add_middleware(