From 30c176733da84b804d77aa9cde3edf3907bd186f Mon Sep 17 00:00:00 2001 From: hailt Date: Thu, 12 Nov 2026 22:08:07 +0700 Subject: [PATCH] fix: proxy production API through frontend --- Dockerfile | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8ee6f96..28540b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /app # Biến môi trường Vite (đọc từ .env trên prod lúc compose build). # Đổi API URL -> build lại là nhận (compose up --build tự làm). -ARG VITE_API_URL=http://localhost:8000/api/v1 +ARG VITE_API_URL=/api/v1 ARG VITE_APP_NAME=danable ARG VITE_USE_MOCK_DATA=true ENV VITE_API_URL=$VITE_API_URL \ @@ -21,7 +21,7 @@ RUN npm run build # ---- stage 2: serve tĩnh bằng nginx ---- FROM nginx:alpine COPY --from=build /app/dist /usr/share/nginx/html -# Ghi đè default conf: SPA fallback + cache lâu cho /assets. +# Ghi đè default conf: same-origin API proxy, SPA fallback + cache lâu cho /assets. # ($$ trong Dockerfile -> $ trong file config.) RUN printf '%s\n' \ 'server {' \ @@ -29,6 +29,12 @@ RUN printf '%s\n' \ ' server_name _;' \ ' root /usr/share/nginx/html;' \ ' index index.html;' \ + ' location /api/ {' \ + ' proxy_pass http://be:8000;' \ + ' proxy_set_header Host $host;' \ + ' proxy_set_header X-Real-IP $remote_addr;' \ + ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \ + ' }' \ ' location / {' \ ' try_files $uri $uri/ /index.html;' \ ' }' \