nginx.conf 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 根目录配置
  5. root /usr/share/nginx/html;
  6. index index.html;
  7. # 禁用缓存,便于开发和调试
  8. add_header Cache-Control "no-cache, no-store, must-revalidate";
  9. add_header Pragma "no-cache";
  10. add_header Expires "0";
  11. # 健康检查端点
  12. location /health {
  13. access_log off;
  14. return 200 "healthy\n";
  15. add_header Content-Type text/plain;
  16. }
  17. # 静态资源配置(带缓存)
  18. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  19. expires 1y;
  20. add_header Cache-Control "public, immutable";
  21. add_header Access-Control-Allow-Origin "*";
  22. }
  23. # 反向代理到API网关
  24. location /api/ {
  25. proxy_pass http://localhost:8080/api/;
  26. proxy_set_header Host $host;
  27. proxy_set_header X-Real-IP $remote_addr;
  28. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  29. proxy_set_header X-Forwarded-Proto $scheme;
  30. proxy_read_timeout 30s;
  31. proxy_connect_timeout 10s;
  32. }
  33. # Vue Router SPA 配置 - 所有非API请求都返回 index.html
  34. location / {
  35. try_files $uri $uri/ /index.html;
  36. }
  37. # 禁用特定文件访问
  38. location ~ /\. {
  39. deny all;
  40. access_log off;
  41. log_not_found off;
  42. }
  43. location ~ ~$ {
  44. deny all;
  45. access_log off;
  46. log_not_found off;
  47. }
  48. # 错误页面配置
  49. error_page 404 /index.html;
  50. error_page 500 502 503 504 /50x.html;
  51. location = /50x.html {
  52. root /usr/share/nginx/html;
  53. }
  54. # 日志配置
  55. access_log /var/log/nginx/access.log;
  56. error_log /var/log/nginx/error.log warn;
  57. }