From 671bb3f655a430da1a59bdbd1fb9a15f6b56c212 Mon Sep 17 00:00:00 2001 From: Tilumca Date: Wed, 14 Oct 2026 04:46:58 +0200 Subject: [PATCH] =?UTF-8?q?Front:=20same-origin=20=E2=80=94=20nginx/vite?= =?UTF-8?q?=20proxy=20/api=20i=20/health=20do=20API=20(apiUrl=20pusty,=20w?= =?UTF-8?q?spolna=20siec=20fitos-net)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/.env.example | 6 +++-- admin/Dockerfile | 6 ++++- admin/docker-compose.yml | 10 +++++++++ admin/nginx.conf | 15 ------------- admin/nginx.conf.template | 40 ++++++++++++++++++++++++++++++++++ admin/public/config.js | 6 +++-- admin/vite.config.ts | 6 +++++ api/docker-compose.yml | 9 ++++++++ app/.env.example | 6 +++-- app/Dockerfile | 6 ++++- app/docker-compose.yml | 10 +++++++++ app/nginx.conf | 21 ------------------ app/nginx.conf.template | 46 +++++++++++++++++++++++++++++++++++++++ app/public/config.js | 6 +++-- app/vite.config.ts | 6 +++++ 15 files changed, 153 insertions(+), 46 deletions(-) delete mode 100644 admin/nginx.conf create mode 100644 admin/nginx.conf.template delete mode 100644 app/nginx.conf create mode 100644 app/nginx.conf.template diff --git a/admin/.env.example b/admin/.env.example index ebc8f4d..401195f 100644 --- a/admin/.env.example +++ b/admin/.env.example @@ -1,2 +1,4 @@ -# Build-time fallback (runtime config.js takes precedence) -VITE_API_URL=http://tisachi.ddns.net:9002 +# Domyślnie nic nie ustawiaj: API jest proxywane w tym samym originsie co SPA +# (nginx w kontenerze / vite dev proxy), a runtime config (public/config.js) ma pusty apiUrl. +# VITE_API_URL tylko dla awaryjnego wskazania zewnętrznego adresu API (build-time fallback). +# VITE_API_URL=http://localhost:9002 diff --git a/admin/Dockerfile b/admin/Dockerfile index 47fe348..6044bd2 100644 --- a/admin/Dockerfile +++ b/admin/Dockerfile @@ -6,6 +6,10 @@ COPY . . RUN npm run build FROM nginx:alpine -COPY nginx.conf /etc/nginx/conf.d/default.conf +# template -> /etc/nginx/templates/: entrypoint obrazu robi envsubst ${API_UPSTREAM} +# i zapisuje wynik do /etc/nginx/conf.d/default.conf (nadpisuje stockowy) +COPY nginx.conf.template /etc/nginx/templates/default.conf.template +# default dla stosu deploy (siec compose); dev nadpisuje przez compose (fitos-api:9002) +ENV API_UPSTREAM=api:9002 COPY --from=build /app/dist /usr/share/nginx/html EXPOSE 9000 diff --git a/admin/docker-compose.yml b/admin/docker-compose.yml index e8230c0..64622a5 100644 --- a/admin/docker-compose.yml +++ b/admin/docker-compose.yml @@ -1,7 +1,17 @@ +# Wymaga wczesniej: docker network create fitos-net (patrz README) services: admin: build: . container_name: fitos-admin ports: - "9000:9000" + environment: + # nginx proxyje /api i /health do API w wspoldzielonej sieci (same-origin) + API_UPSTREAM: fitos-api:9002 + networks: + - fitos-net restart: unless-stopped + +networks: + fitos-net: + external: true diff --git a/admin/nginx.conf b/admin/nginx.conf deleted file mode 100644 index 7ca33ae..0000000 --- a/admin/nginx.conf +++ /dev/null @@ -1,15 +0,0 @@ -server { - listen 9000; - server_name _; - root /usr/share/nginx/html; - index index.html; - - location ~ ^/(index\.html|config\.js|manifest\.json)$ { - add_header Cache-Control "no-cache"; - try_files $uri /index.html; - } - - location / { - try_files $uri /index.html; - } -} diff --git a/admin/nginx.conf.template b/admin/nginx.conf.template new file mode 100644 index 0000000..1c1ea6d --- /dev/null +++ b/admin/nginx.conf.template @@ -0,0 +1,40 @@ +# Konfiguracja nginx jako template — oficjalny entrypoint obrazu nginx robi envsubst +# WYLACZNIE po zdefiniowanych zmiennych srodowiska (API_UPSTREAM), a wynik zapisuje +# do /etc/nginx/conf.d/default.conf. Zmienne nginx ($uri, $host, ...) sa bezpieczne. +# API_UPSTREAM przykladowo: fitos-api:9002 (dev, wspolna siec fitos-net) lub api:9002 +# (stos deploy — ta sama siec compose). +server { + listen 9000; + server_name _; + root /usr/share/nginx/html; + index index.html; + + location ~ ^/(index\.html|config\.js|manifest\.json)$ { + add_header Cache-Control "no-cache"; + try_files $uri /index.html; + } + + # same-origin: API przez ten sam origin co SPA — ciastko refresh (SameSite=Lax) + # zawsze leci, brak CORS. Naglowki X-Forwarded-* sa wymagane przez trust proxy + # w Expressie (limity per-IP liczona po adresie klienta, nie nginxa). + location /api/ { + # upload zdjec do 5 MB + zapas + client_max_body_size 10m; + proxy_pass http://${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; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location = /health { + proxy_pass http://${API_UPSTREAM}; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location / { + try_files $uri /index.html; + } +} diff --git a/admin/public/config.js b/admin/public/config.js index f9c4712..3a5255d 100644 --- a/admin/public/config.js +++ b/admin/public/config.js @@ -1,5 +1,7 @@ // Runtime configuration (no rebuild needed). -// Always use the DDNS domain so the app works from any device (and locally, via router hairpin). +// Same-origin: API jest proxywane przez nginx/vite pod tym samym originem co SPA, +// wiec pusty apiUrl → fallback do window.location.origin (src/config.ts). +// Wystawienie osobnego API tylko dla awaryjnych scenariuszy — wpisz pelny URL. window.__FITOS_CONFIG__ = { - apiUrl: 'http://tisachi.ddns.net:9002', + apiUrl: '', }; diff --git a/admin/vite.config.ts b/admin/vite.config.ts index 9d57f71..c94142b 100644 --- a/admin/vite.config.ts +++ b/admin/vite.config.ts @@ -7,5 +7,11 @@ export default defineConfig({ server: { port: 5174, host: true, + // same-origin jak na produkcji: frontend woła względne /api i /health, + // vite dev proxy przekazuje na lokalne API (musi stać: docker compose w api/) + proxy: { + '/api': 'http://localhost:9002', + '/health': 'http://localhost:9002', + }, }, }); diff --git a/api/docker-compose.yml b/api/docker-compose.yml index 2cc5660..435d0f8 100644 --- a/api/docker-compose.yml +++ b/api/docker-compose.yml @@ -30,6 +30,11 @@ services: retries: 5 start_period: 30s restart: unless-stopped + # fitos-net — widocznosc dla nginxa frontow (app/admin proxyja /api i /health); + # wczesniej: docker network create fitos-net (patrz README) + networks: + - default + - fitos-net db: image: postgres:16-alpine @@ -51,6 +56,10 @@ services: retries: 5 restart: unless-stopped +networks: + fitos-net: + external: true + volumes: fitos_pgdata: fitos_uploads: diff --git a/app/.env.example b/app/.env.example index ebc8f4d..401195f 100644 --- a/app/.env.example +++ b/app/.env.example @@ -1,2 +1,4 @@ -# Build-time fallback (runtime config.js takes precedence) -VITE_API_URL=http://tisachi.ddns.net:9002 +# Domyślnie nic nie ustawiaj: API jest proxywane w tym samym originsie co SPA +# (nginx w kontenerze / vite dev proxy), a runtime config (public/config.js) ma pusty apiUrl. +# VITE_API_URL tylko dla awaryjnego wskazania zewnętrznego adresu API (build-time fallback). +# VITE_API_URL=http://localhost:9002 diff --git a/app/Dockerfile b/app/Dockerfile index ae88213..62c0c7a 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -6,6 +6,10 @@ COPY . . RUN npm run build FROM nginx:alpine -COPY nginx.conf /etc/nginx/conf.d/default.conf +# template -> /etc/nginx/templates/: entrypoint obrazu robi envsubst ${API_UPSTREAM} +# i zapisuje wynik do /etc/nginx/conf.d/default.conf (nadpisuje stockowy) +COPY nginx.conf.template /etc/nginx/templates/default.conf.template +# default dla stosu deploy (siec compose); dev nadpisuje przez compose (fitos-api:9002) +ENV API_UPSTREAM=api:9002 COPY --from=build /app/dist /usr/share/nginx/html EXPOSE 9001 diff --git a/app/docker-compose.yml b/app/docker-compose.yml index 8409579..050f8f9 100644 --- a/app/docker-compose.yml +++ b/app/docker-compose.yml @@ -1,7 +1,17 @@ +# Wymaga wczesniej: docker network create fitos-net (patrz README) services: app: build: . container_name: fitos-app ports: - "9001:9001" + environment: + # nginx proxyje /api i /health do API w wspoldzielonej sieci (same-origin) + API_UPSTREAM: fitos-api:9002 + networks: + - fitos-net restart: unless-stopped + +networks: + fitos-net: + external: true diff --git a/app/nginx.conf b/app/nginx.conf deleted file mode 100644 index bdf4af0..0000000 --- a/app/nginx.conf +++ /dev/null @@ -1,21 +0,0 @@ -server { - listen 9001; - server_name _; - root /usr/share/nginx/html; - index index.html; - - # dev: zero cache'owania JS/CSS/HTML — aktualizacje widoczne od razu po rebuildzie - # (Vite generuje hashowane nazwy assetów, więc long-term cache byłby bezpieczne, - # ale utrudnia weryfikację zmian na telefonie) - add_header Cache-Control "no-store, max-age=0" always; - add_header Pragma "no-cache" always; - add_header Expires "0" always; - - location ~ ^/(index\.html|config\.js|manifest\.json)$ { - try_files $uri /index.html; - } - - location / { - try_files $uri /index.html; - } -} diff --git a/app/nginx.conf.template b/app/nginx.conf.template new file mode 100644 index 0000000..80e8e14 --- /dev/null +++ b/app/nginx.conf.template @@ -0,0 +1,46 @@ +# Konfiguracja nginx jako template — oficjalny entrypoint obrazu nginx robi envsubst +# WYLACZNIE po zdefiniowanych zmiennych srodowiska (API_UPSTREAM), a wynik zapisuje +# do /etc/nginx/conf.d/default.conf. Zmienne nginx ($uri, $host, ...) sa bezpieczne. +# API_UPSTREAM przykladowo: fitos-api:9002 (dev, wspolna siec fitos-net) lub api:9002 +# (stos deploy — ta sama siec compose). +server { + listen 9001; + server_name _; + root /usr/share/nginx/html; + index index.html; + + # dev: zero cache'owania JS/CSS/HTML — aktualizacje widoczne od razu po rebuildzie + # (Vite generuje hashowane nazwy assetów, więc long-term cache byłby bezpieczne, + # ale utrudnia weryfikację zmian na telefonie) + add_header Cache-Control "no-store, max-age=0" always; + add_header Pragma "no-cache" always; + add_header Expires "0" always; + + location ~ ^/(index\.html|config\.js|manifest\.json)$ { + try_files $uri /index.html; + } + + # same-origin: API przez ten sam origin co SPA — ciastko refresh (SameSite=Lax) + # zawsze leci, brak CORS. Naglowki X-Forwarded-* sa wymagane przez trust proxy + # w Expressie (limity per-IP liczona po adresie klienta, nie nginxa). + location /api/ { + # upload zdjec do 5 MB + zapas + client_max_body_size 10m; + proxy_pass http://${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; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location = /health { + proxy_pass http://${API_UPSTREAM}; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location / { + try_files $uri /index.html; + } +} diff --git a/app/public/config.js b/app/public/config.js index f9c4712..3a5255d 100644 --- a/app/public/config.js +++ b/app/public/config.js @@ -1,5 +1,7 @@ // Runtime configuration (no rebuild needed). -// Always use the DDNS domain so the app works from any device (and locally, via router hairpin). +// Same-origin: API jest proxywane przez nginx/vite pod tym samym originem co SPA, +// wiec pusty apiUrl → fallback do window.location.origin (src/config.ts). +// Wystawienie osobnego API tylko dla awaryjnych scenariuszy — wpisz pelny URL. window.__FITOS_CONFIG__ = { - apiUrl: 'http://tisachi.ddns.net:9002', + apiUrl: '', }; diff --git a/app/vite.config.ts b/app/vite.config.ts index f58f980..04d0ea3 100644 --- a/app/vite.config.ts +++ b/app/vite.config.ts @@ -7,5 +7,11 @@ export default defineConfig({ server: { port: 5173, host: true, + // same-origin jak na produkcji: frontend woła względne /api i /health, + // vite dev proxy przekazuje na lokalne API (musi stać: docker compose w api/) + proxy: { + '/api': 'http://localhost:9002', + '/health': 'http://localhost:9002', + }, }, });