| server { |
| listen 80; |
| server_name _; |
| |
| # 开启Gzip压缩(可选但推荐) |
| gzip on; |
| gzip_types text/plain application/javascript application/x-javascript text/css; |
| |
| # 静态资源缓存优化 |
| location ~* \.(js|css|jpg|jpeg|png|gif|ico|svg|woff2)$ { |
| root /usr/share/nginx/html; |
| expires 1y; |
| add_header Cache-Control "public, no-transform"; |
| try_files $uri =404; |
| } |
| |
| # 前端SPA路由处理 |
| location / { |
| root /usr/share/nginx/html; |
| index index.html; |
| try_files $uri $uri/ /index.html; |
| } |
| |
| # API代理 |
| location /api/ { |
| # Docker容器服务名解析 |
| resolver 127.0.0.11 valid=30s; # Docker内置DNS |
| |
| proxy_pass http://thunderhub-backend:5004$request_uri; |
| 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 30s; |
| proxy_read_timeout 90s; |
| |
| # WebSocket支持(如果后端需要) |
| proxy_http_version 1.1; |
| proxy_set_header Upgrade $http_upgrade; |
| proxy_set_header Connection "upgrade"; |
| } |
| |
| # 禁止访问隐藏文件 |
| location ~ /\. { |
| deny all; |
| return 404; |
| } |
| } |