From e79afddd767b9086f3b2ecabd7d8886db3e8ee11 Mon Sep 17 00:00:00 2001 From: mahmood Date: Thu, 26 Nov 2026 14:28:15 +0330 Subject: [PATCH] fix(nginx): proxy every /api/ URL instead of serving it as a static file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nginx picks a regex location over a plain prefix location, so with `location /api/` the static-asset rule (\.png$ …) won any API request whose path ended in an image extension: /api/img/.png?w=400 was looked up under /usr/share/nginx/html and 404ed instead of reaching the API. The same file at /uploads/.png still loaded, which is exactly why an uploaded card thumbnail showed broken on the server but opened fine at full size — and why only .png/.jpg/.gif/.svg uploads were affected while .webp ones looked fine. ^~ stops the regex pass. Verified in an nginx:alpine container with this config: /api/img/abc.png?w=400 now reaches the proxy (502 without an upstream) instead of 404, while bundled assets still come from disk. Co-Authored-By: Claude Haiku 5.5 --- apps/admin-panel/nginx.conf | 10 +++++++++- apps/user-app-v2/nginx.conf | 10 +++++++++- nginx-spa.conf | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/admin-panel/nginx.conf b/apps/admin-panel/nginx.conf index 554a8dc4..11a7a3cc 100644 --- a/apps/admin-panel/nginx.conf +++ b/apps/admin-panel/nginx.conf @@ -30,7 +30,15 @@ server { proxy_set_header X-Forwarded-Proto $scheme; } - location /api/ { + # ^~ is REQUIRED, not cosmetic: nginx picks a regex location over a plain + # prefix one. With `location /api/` the static regex below (\.png$ …) won + # every request whose path ends in an image/bundle extension, so + # /api/img/.png?w=400 was looked up under /usr/share/nginx/html and + # 404ed instead of reaching the API — while the same file at + # /uploads/.png still loaded. That is exactly how a card thumbnail + # came up broken on the server but fine when opened at full size. ^~ + # stops the regex pass, so every /api/ URL is proxied. + location ^~ /api/ { set $api_upstream http://goldbank-api:3000; proxy_pass $api_upstream$request_uri; proxy_http_version 1.1; diff --git a/apps/user-app-v2/nginx.conf b/apps/user-app-v2/nginx.conf index 5beaac51..8737ab78 100644 --- a/apps/user-app-v2/nginx.conf +++ b/apps/user-app-v2/nginx.conf @@ -30,7 +30,15 @@ server { proxy_set_header X-Forwarded-Proto $scheme; } - location /api/ { + # ^~ is REQUIRED, not cosmetic: nginx picks a regex location over a plain + # prefix one. With `location /api/` the static regex below (\.png$ …) won + # every request whose path ends in an image/bundle extension, so + # /api/img/.png?w=400 was looked up under /usr/share/nginx/html and + # 404ed instead of reaching the API — while the same file at + # /uploads/.png still loaded. That is exactly how a card thumbnail + # came up broken on the server but fine when opened at full size. ^~ + # stops the regex pass, so every /api/ URL is proxied. + location ^~ /api/ { set $api_upstream http://goldbank-api:3000; proxy_pass $api_upstream$request_uri; proxy_http_version 1.1; diff --git a/nginx-spa.conf b/nginx-spa.conf index 204497a4..aaad7784 100644 --- a/nginx-spa.conf +++ b/nginx-spa.conf @@ -9,7 +9,10 @@ server { # any real photo with a bare 413 before the request ever reached it. client_max_body_size 20m; - location /api/ { + # ^~ keeps the static-asset regex below from swallowing API URLs that end + # in .png/.jpg/.svg (e.g. /api/img/.png?w=400), which nginx would + # otherwise look up on disk and 404 instead of proxying. + location ^~ /api/ { proxy_pass http://api:3000; proxy_http_version 1.1; proxy_set_header Host $host;