Spaces:
Running
Running
fix: use nginx reverse proxy to forward /api/* to backend at runtime
Browse filesHuggingFace Spaces secrets are runtime env vars, not build-time args,
so Vite cannot bake VITE_API_BASE_URL into the bundle from HF secrets.
Instead, nginx proxies /api/* to ${VITE_API_BASE_URL}/api/* using
envsubst at container startup, keeping the frontend using relative paths.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- Dockerfile +13 -9
Dockerfile
CHANGED
|
@@ -5,14 +5,10 @@ RUN corepack enable && corepack prepare pnpm@latest --activate
|
|
| 5 |
|
| 6 |
WORKDIR /app
|
| 7 |
|
| 8 |
-
COPY pnpm-workspace.yaml package.json pnpm-lock.json* ./
|
| 9 |
RUN pnpm install --no-frozen-lockfile
|
| 10 |
|
| 11 |
COPY . .
|
| 12 |
-
|
| 13 |
-
ARG VITE_API_BASE_URL
|
| 14 |
-
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
|
| 15 |
-
|
| 16 |
RUN pnpm build
|
| 17 |
|
| 18 |
# Stage 2: Serve
|
|
@@ -20,16 +16,24 @@ FROM nginx:alpine
|
|
| 20 |
|
| 21 |
COPY --from=builder /app/dist /usr/share/nginx/html
|
| 22 |
|
| 23 |
-
#
|
| 24 |
RUN printf 'server {\n\
|
| 25 |
listen 7860;\n\
|
| 26 |
root /usr/share/nginx/html;\n\
|
| 27 |
index index.html;\n\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
location / {\n\
|
| 29 |
try_files $uri $uri/ /index.html;\n\
|
| 30 |
}\n\
|
| 31 |
-
}\n' > /etc/nginx/conf.d/default.conf
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
| 5 |
|
| 6 |
WORKDIR /app
|
| 7 |
|
| 8 |
+
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml* pnpm-lock.json* ./
|
| 9 |
RUN pnpm install --no-frozen-lockfile
|
| 10 |
|
| 11 |
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
RUN pnpm build
|
| 13 |
|
| 14 |
# Stage 2: Serve
|
|
|
|
| 16 |
|
| 17 |
COPY --from=builder /app/dist /usr/share/nginx/html
|
| 18 |
|
| 19 |
+
# nginx config template — uses VITE_API_BASE_URL injected at runtime via envsubst
|
| 20 |
RUN printf 'server {\n\
|
| 21 |
listen 7860;\n\
|
| 22 |
root /usr/share/nginx/html;\n\
|
| 23 |
index index.html;\n\
|
| 24 |
+
\n\
|
| 25 |
+
location /api/ {\n\
|
| 26 |
+
proxy_pass ${VITE_API_BASE_URL}/api/;\n\
|
| 27 |
+
proxy_set_header Host $http_host;\n\
|
| 28 |
+
proxy_set_header X-Real-IP $remote_addr;\n\
|
| 29 |
+
}\n\
|
| 30 |
+
\n\
|
| 31 |
location / {\n\
|
| 32 |
try_files $uri $uri/ /index.html;\n\
|
| 33 |
}\n\
|
| 34 |
+
}\n' > /etc/nginx/conf.d/default.conf.template
|
| 35 |
|
| 36 |
+
# Entrypoint: substitute env vars into nginx config, then start nginx
|
| 37 |
+
CMD ["/bin/sh", "-c", "envsubst '$VITE_API_BASE_URL' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]
|
| 38 |
|
| 39 |
+
EXPOSE 7860
|