From c88da44db01590f3cc1967801d0ea2873786ec88 Mon Sep 17 00:00:00 2001 From: wood911010 Date: Sat, 9 Aug 2025 23:32:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20CI=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release_frontend.yml | 2 + Dockerfile | 24 -------- Dockerfile.frontend | 8 +-- docker-compose.prod.yml | 2 - docker-compose.yml | 1 - frontend/entrypoint/10-envsubst.sh | 18 ------ nginx-ci.conf | 76 ++++++++++++++++++++++++++ nginx.conf | 5 +- 8 files changed, 85 insertions(+), 51 deletions(-) delete mode 100644 Dockerfile delete mode 100644 frontend/entrypoint/10-envsubst.sh create mode 100644 nginx-ci.conf diff --git a/.github/workflows/release_frontend.yml b/.github/workflows/release_frontend.yml index 26ec270..6469a2d 100644 --- a/.github/workflows/release_frontend.yml +++ b/.github/workflows/release_frontend.yml @@ -47,6 +47,8 @@ jobs: with: context: . file: ./Dockerfile.frontend + build-args: | + NGINX_CONF=nginx-ci.conf platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 84f646c..0000000 --- a/Dockerfile +++ /dev/null @@ -1,24 +0,0 @@ -# Multi-stage build -FROM node:18-alpine AS frontend-builder -WORKDIR /app/frontend -COPY frontend/package*.json ./ -RUN npm install -COPY frontend/ ./ -RUN npm run build - -FROM golang:1.23-alpine AS backend-builder -WORKDIR /app/backend -COPY backend/go.mod backend/go.sum ./ -RUN go mod download -COPY backend/ ./ -RUN go build -o license-manager cmd/main.go - -FROM alpine:latest -RUN apk --no-cache add ca-certificates -WORKDIR /app -COPY --from=backend-builder /app/backend/license-manager . -COPY --from=frontend-builder /app/frontend/dist ./static -COPY backend/configs/config.example.yaml ./config.yaml -COPY backend/configs/i18n/ ./configs/i18n/ -EXPOSE 18888 -CMD ["./license-manager"] \ No newline at end of file diff --git a/Dockerfile.frontend b/Dockerfile.frontend index 203504a..2cc6829 100644 --- a/Dockerfile.frontend +++ b/Dockerfile.frontend @@ -22,10 +22,10 @@ FROM nginx:alpine # Copy built assets COPY --from=builder /app/dist /usr/share/nginx/html -# Copy nginx configuration template and entrypoint to render from env -COPY nginx.conf /etc/nginx/conf.d/default.conf.template -COPY frontend/entrypoint/10-envsubst.sh /docker-entrypoint.d/10-envsubst.sh -RUN chmod +x /docker-entrypoint.d/10-envsubst.sh +# Selectable nginx configuration (default: nginx.conf). Use build-arg to switch. +ARG NGINX_CONF=nginx.conf +ENV NGINX_CONF=${NGINX_CONF} +COPY ${NGINX_CONF} /etc/nginx/conf.d/default.conf # Create log directory RUN mkdir -p /var/log/nginx diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 18d867e..55114b3 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -87,8 +87,6 @@ services: restart: unless-stopped environment: - TZ=Asia/Shanghai - # Set API upstream for automated deployments (e.g., external gateway or backend host) - - API_UPSTREAM=${API_UPSTREAM:-http://backend:18888} ports: - "${FRONTEND_PORT:-80}:80" depends_on: diff --git a/docker-compose.yml b/docker-compose.yml index a8aad7f..5acdd0d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -68,7 +68,6 @@ services: restart: unless-stopped environment: - TZ=Asia/Shanghai - - API_UPSTREAM=http://backend:18888 ports: - "18080:80" depends_on: diff --git a/frontend/entrypoint/10-envsubst.sh b/frontend/entrypoint/10-envsubst.sh deleted file mode 100644 index 10fc374..0000000 --- a/frontend/entrypoint/10-envsubst.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -set -eu - -# Default upstream if not provided (docker compose dev network) -: "${API_UPSTREAM:=http://backend:18888}" - -TEMPLATE="/etc/nginx/conf.d/default.conf.template" -TARGET="/etc/nginx/conf.d/default.conf" - -if [ -f "$TEMPLATE" ]; then - echo "[entrypoint] Rendering nginx config with API_UPSTREAM=$API_UPSTREAM" - envsubst '\$API_UPSTREAM' < "$TEMPLATE" > "$TARGET" -fi - -exit 0 - - - diff --git a/nginx-ci.conf b/nginx-ci.conf new file mode 100644 index 0000000..3af9641 --- /dev/null +++ b/nginx-ci.conf @@ -0,0 +1,76 @@ +server { + listen 80; + listen [::]:80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + # Gzip compression + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_comp_level 6; + gzip_types + text/plain + text/css + text/xml + text/javascript + application/javascript + application/xml+rss + application/json + image/svg+xml; + + # Security headers + add_header X-Frame-Options DENY; + add_header X-Content-Type-Options nosniff; + add_header X-XSS-Protection "1; mode=block"; + add_header Referrer-Policy "strict-origin-when-cross-origin"; + + # Handle Vue Router history mode + location / { + try_files $uri $uri/ /index.html; + } + + # Cache static assets + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + access_log off; + } + + # API proxy to backend on localhost (for CI/standalone frontend deployments) + location /api/ { + proxy_pass http://127.0.0.1:18888; + 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_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + + # CORS headers passthrough + proxy_pass_header Access-Control-Allow-Origin; + proxy_pass_header Access-Control-Allow-Methods; + proxy_pass_header Access-Control-Allow-Headers; + proxy_pass_header Access-Control-Allow-Credentials; + + # Logs for debugging + access_log /var/log/nginx/api_proxy.log; + error_log /var/log/nginx/api_proxy_error.log; + } + + # Disable access to hidden files + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } + + # Custom error pages + error_page 404 /index.html; + error_page 500 502 503 504 /index.html; +} + + diff --git a/nginx.conf b/nginx.conf index 18274c4..9dd2a09 100644 --- a/nginx.conf +++ b/nginx.conf @@ -39,9 +39,10 @@ server { access_log off; } - # API proxy to backend (configurable via $API_UPSTREAM) + # API proxy to backend (container network) location /api/ { - proxy_pass $API_UPSTREAM; + # Keep original request path; backend service name resolves inside Docker network + proxy_pass http://backend:18888; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;