From 1c6f0b5b6170522e469eff3a70ebc99da00fccee Mon Sep 17 00:00:00 2001 From: Luca Sacchi Ricciardi Date: Wed, 8 Apr 2026 00:05:14 +0200 Subject: [PATCH] fix: configure nginx proxy for API calls to fix cross-origin issues Problem: Frontend was calling localhost:8000 for API, but when accessing from Windows (192.168.254.79:8888), the browser tried to connect to localhost on the Windows machine instead of the server. Solution: 1. Added nginx.conf with proxy configuration for /api/ routes 2. Updated Dockerfile to copy nginx.conf to container 3. Changed api.ts baseURL from 'http://localhost:8000/api/v1' to '/api/v1' 4. Nginx now proxies all /api/ requests to backend:8000 This allows the frontend to work correctly from any client by using relative paths that nginx proxies to the backend. --- frontend/Dockerfile | 4 ++-- frontend/nginx.conf | 45 +++++++++++++++++++++++++++++++++++++++++ frontend/src/lib/api.ts | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 frontend/nginx.conf diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 1e8f52d..f2dfe40 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -23,8 +23,8 @@ FROM nginx:alpine # Copy built assets COPY --from=builder /app/dist /usr/share/nginx/html -# Copy nginx config (optional) -# COPY nginx.conf /etc/nginx/conf.d/default.conf +# Copy nginx config with API proxy +COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..b3cb309 --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,45 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + index index.html; + + # Gzip compression + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json; + + # Proxy API requests to backend + location /api/ { + proxy_pass http://backend:8000/api/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + 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; + proxy_cache_bypass $http_upgrade; + } + + # Serve static files + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + try_files $uri =404; + } + + # Handle client-side routing - serve index.html for all routes + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + } + + # Health check endpoint + location /health { + access_log off; + return 200 "healthy\n"; + add_header Content-Type text/plain; + } +} diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 30694a5..5b0492d 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1,7 +1,7 @@ import axios from 'axios'; const api = axios.create({ - baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8000/api/v1', + baseURL: '/api/v1', headers: { 'Content-Type': 'application/json', },