# ===================================================================== # 翼云Hub · LAOP-web 前端 nginx 配置 # -------------------------------------------------------------------- # 职责: # 1) SPA 静态资源服务 + History 路由 fallback # 2) 反向代理前端 axios 请求(baseURL=/api)到后端容器 laop-admin:8081 # 3) 反向代理后端静态文件(/static)到后端 laop-admin:8081/laop/static # -------------------------------------------------------------------- # 后端 context-path = /laop;controller 路径分两类: # - 带 /api 前缀:/api/file, /api/mini/*, /api/wechat/mp → 完整路径 /laop/api/... # - 不带 /api 前缀:/user, /banner, /course ... → 完整路径 /laop/... # 前端 axios baseURL=/api,所以请求分两种: # - /api/file/xxx, /api/mini/xxx, /api/wechat/xxx → 重写为 /laop/api/... # - /api/user/xxx 等其余 → 重写为 /laop/... # 用 nginx location 精确匹配实现,避免使用 if(nginx "if is evil") # ===================================================================== server { listen 80; server_name _; # 字符集 & 日志 charset utf-8; access_log /var/log/nginx/access.log combined; error_log /var/log/nginx/error.log warn; # SPA 根目录 root /usr/share/nginx/html; index index.html; # ----- 静态资源缓存(带 hash 的 assets 长缓存)----- location ~* \.(?:js|css|woff2?|ttf|otf|eot|png|jpg|jpeg|gif|webp|svg|ico)$ { try_files $uri =404; expires 30d; add_header Cache-Control "public, max-age=2592000, immutable"; access_log off; } # ----- 后端反向代理:文件上传/下载 /api/file/* → /laop/api/file/* ----- location /api/file/ { proxy_pass http://laop-admin:8081/laop/api/file/; proxy_http_version 1.1; 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; # 文件上传放宽超时(与后端 max-request-size 15MB 配合) proxy_connect_timeout 30s; proxy_send_timeout 120s; proxy_read_timeout 120s; client_max_body_size 20m; } # ----- 后端反向代理:小程序端 /api/mini/* → /laop/api/mini/* ----- location /api/mini/ { proxy_pass http://laop-admin:8081/laop/api/mini/; proxy_http_version 1.1; 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; } # ----- 后端反向代理:微信公众号回调 /api/wechat/* → /laop/api/wechat/* ----- location /api/wechat/ { proxy_pass http://laop-admin:8081/laop/api/wechat/; proxy_http_version 1.1; 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; } # ----- 后端反向代理:其余 /api/* → /laop/* ----- # 适配 Web 管理端 controller(路径不带 /api 前缀,如 /user /banner /course 等) location /api/ { proxy_pass http://laop-admin:8081/laop/; proxy_http_version 1.1; 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; } # ----- 后端静态资源(上传的图片/视频/文件)/static/* → /laop/static/* ----- # 前端通过 VITE_FILE_BASE_URL 留空时,浏览器以相对路径访问 /static/xxx location /static/ { proxy_pass http://laop-admin:8081/laop/static/; proxy_http_version 1.1; 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_read_timeout 120s; } # ----- 健康检查 ----- location = /healthz { access_log off; return 200 "ok\n"; add_header Content-Type text/plain; } # ----- SPA History 路由 fallback(兜底放在最后)----- location / { try_files $uri $uri/ /index.html; } }