From b4ed18af71dcabc8650a73d167112d68cc5ca4d7 Mon Sep 17 00:00:00 2001 From: ali_mohaghegh Date: Wed, 2 Sep 2026 18:42:02 +0330 Subject: [PATCH] fix(nginx): resolve api upstream at request time via Docker DNS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After the api container restarts it gets a new IP; nginx cached the old one at startup and kept proxying /api/* to the stale address — which by then belonged to another service (casdoor), so login returned Casdoor's error envelope and the frontend crashed with "Cannot read properties of null (reading 'user')". proxy_pass now uses a variable + resolver 127.0.0.11 so the upstream re-resolves per request. Verified: login works, and still works after restarting the api container. --- frontend/nginx.conf | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontend/nginx.conf b/frontend/nginx.conf index 96a390a..e3f8d02 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -14,8 +14,15 @@ server { client_max_body_size 50m; # API reverse proxy -> FastAPI backend (port 8000) + # NOTE: upstream is resolved at REQUEST time through Docker's embedded DNS + # (127.0.0.11). A plain `proxy_pass http://api:8000` caches the container + # IP at nginx startup, so after the api container restarts and gets a new + # IP, nginx keeps proxying to the stale address (which may belong to + # another service) until this container is restarted too. location /api/ { - proxy_pass http://api:8000; + resolver 127.0.0.11 valid=10s ipv6=off; + set $api_upstream http://api:8000; + proxy_pass $api_upstream; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;