Add MongoDB to the stack and fix nginx upstream resolution
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 11s

The backend image ships no .env (gitignored) and compose passed no env,
so MONGO_URI fell back to the container's own 127.0.0.1, the API died on
connect, and nginx answered 502 for every /api request.

- docker-compose.yml: mongo:7 service with a data volume; backend gets
  MONGO_URI=mongodb://mongo:27017/mathew and waits on its healthcheck
- nginx.conf: resolve service names through Docker's DNS per request
  (stale cached IPs 502 after every container recreate); match bare /api
  too so it cannot loop through the frontend's SSR proxy
This commit is contained in:
2026-10-01 22:56:07 +02:00
parent 1bd971232e
commit 4e80e15703
2 changed files with 23 additions and 27 deletions

View File

@@ -19,23 +19,25 @@ services:
backend: backend:
image: git.aranroig.com/syndria98/mathew-backend:latest image: git.aranroig.com/syndria98/mathew-backend:latest
environment: environment:
PORT: 5000 # The image carries no .env (it is gitignored), and a container's
# The image carries no .env, so without this the backend would try # 127.0.0.1 is itself — with the default URI the API crashed on connect
# 127.0.0.1 inside its own container and crash-loop — nginx then # and nginx answered 502. Mongo is a service of this stack now.
# answers every /api/ request with 502.
MONGO_URI: mongodb://mongo:27017/mathew MONGO_URI: mongodb://mongo:27017/mathew
# Put JWT_SECRET (and AUTH_TOKEN_TTL if you like) in a .env file next
# to this compose file on the server; compose reads it from there.
JWT_SECRET: ${JWT_SECRET:-}
AUTH_TOKEN_TTL: ${AUTH_TOKEN_TTL:-7d}
depends_on: depends_on:
- mongo mongo:
condition: service_healthy
restart: always restart: always
mongo: mongo:
image: mongo:7 image: mongo:7
volumes: volumes:
- mongo-data:/data/db - mongo-data:/data/db
healthcheck:
test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping')"]
interval: 10s
timeout: 5s
retries: 5
start_period: 20s
restart: always restart: always
volumes: volumes:

View File

@@ -1,26 +1,20 @@
events {} events {}
http { http {
# Docker's built-in DNS. With the upstreams below held in variables, # Docker's embedded DNS: resolve service names per request (cached 10s).
# nginx re-resolves the service names at request time instead of caching # nginx would otherwise resolve them once at startup and keep proxying to
# the address from startup — so a recreated container (new IP) does not # a recreated container's old IP — a 502 until nginx itself is reloaded.
# leave nginx proxying to a stale address (502).
resolver 127.0.0.11 valid=10s ipv6=off; resolver 127.0.0.11 valid=10s ipv6=off;
# "Connection: upgrade" only for real upgrade requests, not every one.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server { server {
listen 80; listen 80;
server_name _; server_name _;
# Api Requests # Api Requests ("/api" without a trailing slash must land here too,
location /api/ { # or it falls through to the frontend and loops through its SSR proxy)
set $api_upstream http://backend:5000; location /api {
proxy_pass $api_upstream; set $api_upstream backend:5000;
proxy_pass http://$api_upstream;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
@@ -28,13 +22,13 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection "upgrade";
} }
# Normal requests # Normal requests
location / { location / {
set $frontend_upstream http://frontend:3000; set $frontend_upstream frontend:3000;
proxy_pass $frontend_upstream; proxy_pass http://$frontend_upstream;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
@@ -42,7 +36,7 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection "upgrade";
} }
} }