From 45aeaf32659daaaa1f6a8096fcd765d3f22cd4b3 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Mon, 5 Aug 2024 11:15:52 -0300 Subject: [PATCH] fix: Backend URL redirection logic Fix FastAPI and nginx configuration, to make the application correctly redirect URLs. This is specially useful when URLs ended with forward slash are redirected to their stripped version. Included changes: * Stop removing the `/api` prefix in nginx rewrite rules, so FastAPI knows what's the original URL path being requested. * Use `$http_host` in nginx, so FastAPI receives both the original host and port, to build the redirect URL (as `$host` does not include the port, if present). * Make all FastAPI included routers know their prefix, to correctly route incoming requests. This fix was found based on a report that redirects from URLs ended with forward slash were not working [1]. [1] https://github.com/rommapp/romm/issues/1051#issuecomment-2269049762 --- backend/main.py | 32 ++++++++++++++++---------------- docker/nginx/default.conf | 3 +-- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/backend/main.py b/backend/main.py index 9450acec8..ffc095de8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -89,22 +89,22 @@ app.add_middleware( # Sets context vars in request-response cycle app.middleware("http")(set_context_middleware) -app.include_router(heartbeat.router) -app.include_router(auth.router) -app.include_router(user.router) -app.include_router(platform.router) -app.include_router(rom.router) -app.include_router(search.router) -app.include_router(saves.router) -app.include_router(states.router) -app.include_router(tasks.router) -app.include_router(feeds.router) -app.include_router(config.router) -app.include_router(stats.router) -app.include_router(raw.router) -app.include_router(screenshots.router) -app.include_router(firmware.router) -app.include_router(collections.router) +app.include_router(heartbeat.router, prefix="/api") +app.include_router(auth.router, prefix="/api") +app.include_router(user.router, prefix="/api") +app.include_router(platform.router, prefix="/api") +app.include_router(rom.router, prefix="/api") +app.include_router(search.router, prefix="/api") +app.include_router(saves.router, prefix="/api") +app.include_router(states.router, prefix="/api") +app.include_router(tasks.router, prefix="/api") +app.include_router(feeds.router, prefix="/api") +app.include_router(config.router, prefix="/api") +app.include_router(stats.router, prefix="/api") +app.include_router(raw.router, prefix="/api") +app.include_router(screenshots.router, prefix="/api") +app.include_router(firmware.router, prefix="/api") +app.include_router(collections.router, prefix="/api") app.mount("/ws", socket_handler.socket_app) diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index 7065d1c37..5565cad7b 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -54,7 +54,7 @@ http { listen 8080; server_name localhost; - proxy_set_header Host $host; + proxy_set_header Host $http_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; @@ -79,7 +79,6 @@ http { # Backend api calls location /api { - rewrite /api/(.*) /$1 break; proxy_pass http://wsgi_server; } location /ws {