fix: proxy production API through frontend

This commit is contained in:
hailt 2026-11-12 22:08:07 +07:00
parent b0912d0b21
commit 30c176733d

View file

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