Add multi-arch support and optimize Nginx configuration for improved performance and compatibility

This commit is contained in:
wood chen 2025-02-08 18:47:21 +08:00
parent 4c48a50aca
commit 5c9bb1b2a2
2 changed files with 74 additions and 17 deletions

View File

@ -25,11 +25,12 @@ jobs:
with: with:
go-version: '1.21' go-version: '1.21'
# 构建后端 # 构建后端(使用 Alpine 环境)
- name: Build backend - name: Build backend
run: | run: |
cd backend cd backend
go build -o main . GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main-amd64 .
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o main-arm64 .
# 设置 Node.js 环境 # 设置 Node.js 环境
- name: Set up Node.js - name: Set up Node.js
@ -51,28 +52,75 @@ jobs:
run: | run: |
mkdir -p backend/config mkdir -p backend/config
cat > backend/config/nginx.conf << 'EOL' cat > backend/config/nginx.conf << 'EOL'
worker_processes auto; user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events { events {
worker_connections 1024; worker_connections 1024;
} }
http { http {
include /etc/nginx/mime.types; include /etc/nginx/mime.types;
default_type application/octet-stream; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65; keepalive_timeout 65;
gzip on; gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
location / { root /app/frontend;
root /app/frontend; index index.html;
index index.html;
try_files $uri $uri/ /index.html; # 前端文件缓存设置
location /assets {
expires 7d;
add_header Cache-Control "public, no-transform";
} }
# API 反向代理
location /api { location /api {
proxy_pass http://localhost:8080; proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; 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_cache_bypass $http_upgrade;
proxy_buffering off;
proxy_read_timeout 300s;
}
# SPA 路由支持
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} }
} }
} }
@ -115,12 +163,14 @@ jobs:
${{ env.IMAGE_NAME }}:${{ steps.version.outputs.VERSION }} ${{ env.IMAGE_NAME }}:${{ steps.version.outputs.VERSION }}
${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}:latest
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
- name: 部署到服务器 # 部署到服务器
- name: Deploy to server
uses: appleboy/ssh-action@master uses: appleboy/ssh-action@master
if: github.event_name != 'pull_request'
env: env:
DOCKER_IMAGE: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE_NAME }} DOCKER_IMAGE: ${{ env.IMAGE_NAME }}
PROJECT_PATH: ${{ secrets.PROJECT_PATH }} PROJECT_PATH: ${{ secrets.PROJECT_PATH }}
with: with:
host: ${{ secrets.SERVER_IP }} host: ${{ secrets.SERVER_IP }}
@ -128,7 +178,7 @@ jobs:
key: ${{ secrets.SERVER_SSH_KEY }} key: ${{ secrets.SERVER_SSH_KEY }}
envs: DOCKER_IMAGE,PROJECT_PATH envs: DOCKER_IMAGE,PROJECT_PATH
script: | script: |
docker pull $DOCKER_IMAGE
cd $PROJECT_PATH cd $PROJECT_PATH
docker compose pull
docker compose down docker compose down
docker compose up -d docker compose up -d

View File

@ -15,12 +15,19 @@ RUN apk add --no-cache \
RUN mkdir -p /app/data /app/frontend RUN mkdir -p /app/data /app/frontend
# 复制构建产物 # 复制构建产物
COPY backend/main ./ COPY backend/main-* ./
RUN if [ "$(uname -m)" = "aarch64" ]; then \
cp main-arm64 main; \
else \
cp main-amd64 main; \
fi && \
rm main-* && \
chmod +x main
COPY frontend/dist /app/frontend COPY frontend/dist /app/frontend
COPY backend/config/nginx.conf /etc/nginx/nginx.conf COPY backend/config/nginx.conf /etc/nginx/nginx.conf
COPY scripts/start.sh ./ COPY scripts/start.sh ./
RUN chmod +x start.sh
RUN chmod +x start.sh main
EXPOSE 80 EXPOSE 80