update: 优化 docker

This commit is contained in:
ctwj
2025-07-19 09:45:51 +08:00
parent 5839012962
commit 4fd20c7430
4 changed files with 185 additions and 16 deletions

View File

@@ -1,12 +1,6 @@
# 前端构建阶段
FROM node:18-alpine AS frontend-builder
FROM node:20-slim AS frontend-builder
# 安装必要的构建工具Rust工具链
RUN apk add --no-cache --virtual .build-deps \
python3 \
make \
g++ \
&& npm config set python python3
# 安装pnpm
RUN npm install -g pnpm
@@ -44,8 +38,6 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# 后端运行阶段
FROM alpine:latest AS backend
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# 复制后端二进制文件

View File

@@ -4,7 +4,7 @@ services:
postgres:
image: postgres:15-alpine
environment:
POSTGurl_db: url_db
POSTGRES_DB: url_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
@@ -21,8 +21,8 @@ services:
build:
context: .
target: backend
ports:
- "8080:8080"
expose:
- "8080"
environment:
DB_HOST: postgres
DB_PORT: 5432
@@ -35,17 +35,39 @@ services:
condition: service_healthy
volumes:
- ./uploads:/root/uploads
networks:
- app-network
frontend:
build:
context: .
target: frontend
ports:
- "3000:3000"
expose:
- "3000"
environment:
API_BASE: http://localhost:8080/api
API_BASE: http://backend:8080/api
depends_on:
- backend
networks:
- app-network
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
depends_on:
- backend
- frontend
networks:
- app-network
restart: unless-stopped
volumes:
postgres_data:
networks:
app-network:
driver: bridge

105
nginx/conf.d/default.conf Normal file
View File

@@ -0,0 +1,105 @@
upstream backend {
server backend:8080;
}
upstream frontend {
server frontend:3000;
}
server {
listen 80;
server_name localhost;
# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
# API 路由 - 所有 /api/ 开头的请求转发到后端
location /api/ {
proxy_pass http://backend;
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;
# 缓冲设置
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
# 错误处理
proxy_intercept_errors on;
error_page 502 503 504 /50x.html;
}
# 静态文件路由 - 直接转发到前端
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
proxy_pass http://frontend;
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;
# 缓存设置
expires 1y;
add_header Cache-Control "public, immutable";
}
# 上传文件路由 - 直接访问后端的上传目录
location /uploads/ {
proxy_pass http://backend;
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;
}
# 健康检查
location /health {
proxy_pass http://backend/health;
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;
}
# 默认路由 - 所有其他请求转发到前端
location / {
proxy_pass http://frontend;
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;
# WebSocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 缓冲设置
proxy_buffering off;
# 错误处理
proxy_intercept_errors on;
error_page 502 503 504 /50x.html;
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

50
nginx/nginx.conf Normal file
View File

@@ -0,0 +1,50 @@
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 100M;
# Gzip 压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# 包含站点配置
include /etc/nginx/conf.d/*.conf;
}