Front: same-origin — nginx/vite proxy /api i /health do API (apiUrl pusty, wspolna siec fitos-net)

This commit is contained in:
Tilumca
2026-10-14 04:46:58 +02:00
parent 18f1432eac
commit 671bb3f655
15 changed files with 153 additions and 46 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}
}

40
admin/nginx.conf.template Normal file
View File

@@ -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;
}
}

View File

@@ -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: '',
};

View File

@@ -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',
},
},
});

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}
}

46
app/nginx.conf.template Normal file
View File

@@ -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;
}
}

View File

@@ -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: '',
};

View File

@@ -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',
},
},
});