default.conf.template 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name bmsm27.kptyun.com;
  5. gzip on;
  6. gzip_static on;
  7. gzip_comp_level 6;
  8. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  9. # 前端静态资源配置
  10. location / {
  11. root /usr/share/nginx/html;
  12. try_files $uri $uri/ /index.html;
  13. client_max_body_size 300m;
  14. client_body_timeout 20s;
  15. # 静态资源缓存控制
  16. add_header Cache-Control "no-cache, no-store, must-revalidate";
  17. add_header Pragma no-cache;
  18. add_header Expires 0;
  19. }
  20. # 核心:适配 frp 转发的跨域代理配置
  21. location ~* ^/(auth|api)/ {
  22. # 转发到后端真实接口
  23. proxy_pass http://bmsm278082.kptyun.com$request_uri;
  24. # 保留真实请求信息,适配 frp 转发
  25. proxy_set_header Host $http_host;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. proxy_set_header X-Forwarded-Proto $scheme;
  29. proxy_set_header Origin $http_origin; # 关键:保留请求源
  30. # 超时配置,防止 frp 转发超时
  31. proxy_connect_timeout 120s;
  32. proxy_read_timeout 120s;
  33. proxy_send_timeout 120s;
  34. # ========== 跨域核心配置(适配 frp 转发,永不丢失跨域头) ==========
  35. add_header Access-Control-Allow-Origin $http_origin always;
  36. add_header Access-Control-Allow-Credentials true always;
  37. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE, PATCH' always;
  38. add_header Access-Control-Allow-Headers 'Authorization, Content-Type, Token, X-Requested-With, Accept, Origin' always;
  39. add_header Access-Control-Expose-Headers 'Content-Length, Content-Type' always;
  40. # 直接处理 OPTIONS 预检请求,不转发到后端(关键优化)
  41. if ($request_method = 'OPTIONS') {
  42. return 204;
  43. }
  44. }
  45. # 错误页面配置
  46. error_page 500 502 503 504 /50x.html;
  47. location = /50x.html {
  48. root /usr/share/nginx/html;
  49. }
  50. }