server { listen 80; server_name 10.1.20.6; # 启用gzip压缩 gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json application/xml image/svg+xml; gzip_comp_level 6; gzip_disable "msie6"; # 启用brotli压缩(如果nginx支持) # brotli on; # brotli_comp_level 6; # brotli_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json application/xml image/svg+xml; # HTML文件 - 不缓存,确保用户总是获取最新版本 location ~* \.(html|htm)$ { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html =404; add_header Cache-Control "no-cache, no-store, must-revalidate"; add_header Pragma "no-cache"; add_header Expires "0"; } # 前端静态文件根路径 - 用于SPA路由 location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html =404; add_header Cache-Control "no-cache, no-store, must-revalidate"; add_header Pragma "no-cache"; add_header Expires "0"; } # 带hash的JS/CSS文件 - 长期缓存(1年),因为hash变化时文件名会变化 location ~* \.(js|css)$ { root /usr/share/nginx/html; expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # 图片资源 - 长期缓存(1年) location ~* \.(jpg|jpeg|png|gif|ico|svg|webp|avif)$ { root /usr/share/nginx/html; expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # 字体文件 - 长期缓存(1年) location ~* \.(woff|woff2|ttf|eot|otf)$ { root /usr/share/nginx/html; expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # 其他静态资源 - 缓存1个月 location ~* \.(pdf|zip|tar|gz)$ { root /usr/share/nginx/html; expires 1M; add_header Cache-Control "public"; access_log off; } # 代理配置 - 按优先级顺序,更具体的路径放在前面 # trackApi 服务代理 - http://43.139.210.35:8889 location /api/trackApi { rewrite ^/api/trackApi(.*)$ $1 break; proxy_pass http://43.139.210.35:8889; 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_send_timeout 30s; proxy_read_timeout 30s; } # SimpleAdmin 服务 - userCenter - http://43.139.210.35:5566 location /api/userCenter { rewrite ^/api(.*)$ $1 break; proxy_pass http://43.139.210.35:5566; 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_send_timeout 30s; proxy_read_timeout 30s; } # SimpleAdmin 服务 - sys - http://43.139.210.35:5566 location /api/sys { rewrite ^/api(.*)$ $1 break; proxy_pass http://43.139.210.35:5566; 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_send_timeout 30s; proxy_read_timeout 30s; } # 其他 API 请求代理到 8888 端口 - http://43.139.210.35:8888 location /api { rewrite ^/api(.*)$ $1 break; proxy_pass http://43.139.210.35:8888; 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_send_timeout 30s; proxy_read_timeout 30s; } # 错误页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }