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/<hash>.png?w=400 was looked up under /usr/share/nginx/html and 404ed instead of reaching the API. The same file at /uploads/<hash>.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 <noreply@anthropic.com>
96 lines
3.8 KiB
Nginx Configuration File
96 lines
3.8 KiB
Nginx Configuration File
server {
|
|
listen 80;
|
|
server_name _;
|
|
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
# Uploads are capped at 15 MB by the API; nginx's 1 MB default would reject
|
|
# any real photo with a bare 413 before the request ever reached it.
|
|
client_max_body_size 20m;
|
|
|
|
# The first request for a given /api/img width renders it on demand; an
|
|
# 8000px source can take a while on a small VPS, and nginx's 60s default
|
|
# turned that into a 504 — a card thumbnail that stayed broken until the
|
|
# variant finished, while the original (/uploads) opened fine.
|
|
proxy_read_timeout 180s;
|
|
|
|
# Docker's embedded DNS. Without re-resolving at request time, nginx caches the
|
|
# api container's IP from startup and keeps returning 502 ("no live upstreams")
|
|
# after the api container restarts with a new IP.
|
|
resolver 127.0.0.11 valid=10s ipv6=off;
|
|
|
|
location ^~ /api/uploads/ {
|
|
set $api_upstream http://goldbank-api:3000;
|
|
proxy_pass $api_upstream$request_uri;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $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;
|
|
}
|
|
|
|
# ^~ 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/<hash>.png?w=400 was looked up under /usr/share/nginx/html and
|
|
# 404ed instead of reaching the API — while the same file at
|
|
# /uploads/<hash>.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;
|
|
proxy_set_header Host $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;
|
|
}
|
|
|
|
# Product images and other uploads are served by the API (main.ts
|
|
# useStaticAssets at /uploads/), not by this nginx. Without this proxy the
|
|
# ProductImage.url values the UI renders would 404.
|
|
location ^~ /uploads/ {
|
|
set $api_upstream http://goldbank-api:3000;
|
|
proxy_pass $api_upstream$request_uri;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $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;
|
|
}
|
|
|
|
# The files that say which release is live are never cached: index.html
|
|
# (it names the hashed bundles), sw.js (a stale service worker keeps serving
|
|
# the old precache) and version.json (the update gate polls it). They used
|
|
# to fall under the 6-month rule below, which could pin a browser to an old
|
|
# release. Exact matches win over that regex.
|
|
location = /index.html {
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate" always;
|
|
try_files $uri =404;
|
|
}
|
|
location = /sw.js {
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate" always;
|
|
try_files $uri =404;
|
|
}
|
|
location = /version.json {
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate" always;
|
|
try_files $uri =404;
|
|
}
|
|
location = /manifest.webmanifest {
|
|
add_header Cache-Control "no-cache" always;
|
|
try_files $uri =404;
|
|
}
|
|
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
}
|
|
|
|
location ~* \.(?:ico|css|js|gif|jpe?g|png|woff2?|eot|ttf|svg)$ {
|
|
expires 6M;
|
|
access_log off;
|
|
add_header Cache-Control "public";
|
|
}
|
|
}
|