feat(nest-front): add API_URL proxy and VITE_API_URL support for split deployment
- nginx.conf: proxy /api/ to ${API_URL} (injected at container start via envsubst)
- Dockerfile: accept VITE_API_URL build arg; use envsubst CMD to hydrate nginx template
- src/utils/api.ts: export API_BASE using VITE_API_URL or fallback to /api
Enables nest-front to run on Coolify while nest-backend runs on Portainer.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,13 +6,25 @@ COPY package.json package-lock.json ./
|
|||||||
RUN npm ci
|
RUN npm ci
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
# VITE_API_URL is baked into the JS bundle at build time.
|
||||||
|
# Default to /api so the nginx proxy is used when not overridden.
|
||||||
|
ARG VITE_API_URL=/api
|
||||||
|
ENV VITE_API_URL=$VITE_API_URL
|
||||||
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
FROM nginx:alpine
|
FROM nginx:alpine
|
||||||
|
|
||||||
COPY --from=build /app/dist /usr/share/nginx/html
|
COPY --from=build /app/dist /usr/share/nginx/html
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
COPY nginx.conf /etc/nginx/nginx.conf.template
|
||||||
|
|
||||||
EXPOSE 5173
|
EXPOSE 5173
|
||||||
|
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
# API_URL is the backend's public base URL used by nginx proxy_pass.
|
||||||
|
# Set this at runtime in Coolify, e.g. API_URL=https://api.crowmate.fr
|
||||||
|
ENV API_URL=http://localhost:3000
|
||||||
|
|
||||||
|
# Substitute ${API_URL} in the nginx template at container start, then launch nginx.
|
||||||
|
# The quoted variable list prevents envsubst from replacing nginx variables like $host.
|
||||||
|
CMD ["/bin/sh", "-c", "envsubst '${API_URL}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]
|
||||||
|
|||||||
@@ -3,6 +3,13 @@ server {
|
|||||||
root /usr/share/nginx/html;
|
root /usr/share/nginx/html;
|
||||||
index index.html;
|
index index.html;
|
||||||
|
|
||||||
|
location /api/ {
|
||||||
|
proxy_pass ${API_URL}/api/;
|
||||||
|
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 / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
|
|||||||
4
nest-front/src/utils/api.ts
Normal file
4
nest-front/src/utils/api.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// API base URL.
|
||||||
|
// - In Docker (Coolify): nginx proxies /api/* to the backend, so we use a relative path.
|
||||||
|
// - Set VITE_API_URL at build time to call the backend directly (e.g. during local dev without nginx).
|
||||||
|
export const API_BASE = import.meta.env.VITE_API_URL ?? '/api'
|
||||||
Reference in New Issue
Block a user