next.config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import type { NextConfig } from "next";
  2. // 开发时代理目标端口(统一从根目录 .env 读取 NEXT_PUBLIC_BACKEND_*)
  3. const backendPort = process.env.NEXT_PUBLIC_BACKEND_PORT || "8080";
  4. const backendHost = process.env.NEXT_PUBLIC_BACKEND_HOST || "localhost";
  5. const nextConfig: NextConfig = {
  6. // 开发环境:代理 API 请求到后端
  7. // 生产环境:由 Nginx 处理,这个配置不会生效(因为生产环境是静态构建)
  8. async rewrites() {
  9. // 只在开发环境启用代理
  10. if (process.env.NODE_ENV === "development") {
  11. return [
  12. // 形态2(同域 /api)在本地开发的兜底:把 /api/* 代理到后端 /api/*
  13. // 避免 Next 把 /api 当成自己的 API 路由而导致 404
  14. {
  15. source: "/api/:path*",
  16. destination: `http://${backendHost}:${backendPort}/api/:path*`,
  17. },
  18. // 优先匹配后端 API 路径(这些需要代理到后端)
  19. {
  20. source: "/agent/profile/:path*",
  21. destination: `http://${backendHost}:${backendPort}/agent/profile/:path*`,
  22. },
  23. {
  24. source: "/agent/avatar/:path*",
  25. destination: `http://${backendHost}:${backendPort}/agent/avatar/:path*`,
  26. },
  27. {
  28. source: "/agent/embedding-config",
  29. destination: `http://${backendHost}:${backendPort}/agent/embedding-config`,
  30. },
  31. {
  32. source: "/agent/prompts",
  33. destination: `http://${backendHost}:${backendPort}/agent/prompts`,
  34. },
  35. {
  36. source: "/agent/ai-config/:path*",
  37. destination: `http://${backendHost}:${backendPort}/agent/ai-config/:path*`,
  38. },
  39. {
  40. // 数据报表 API(后端 gin 路由在 /agent/analytics/summary)
  41. source: "/agent/analytics/summary",
  42. destination: `http://${backendHost}:${backendPort}/agent/analytics/summary`,
  43. },
  44. {
  45. source: "/agent/logs/api",
  46. destination: `http://${backendHost}:${backendPort}/agent/logs/api`,
  47. },
  48. {
  49. source: "/agent/logs/frontend",
  50. destination: `http://${backendHost}:${backendPort}/agent/logs/frontend`,
  51. },
  52. {
  53. source: "/agent/logs/min-level",
  54. destination: `http://${backendHost}:${backendPort}/agent/logs/min-level`,
  55. },
  56. // 匹配其他 API 路径(不以 /_next、/agent、/api、/chat 开头的路径)
  57. // /api/agent/prompts 由 app/api/agent/prompts/route.ts 代理,不在此转发
  58. {
  59. source: "/:path((?!_next|agent|api|chat|favicon.ico).*)",
  60. destination: `http://${backendHost}:${backendPort}/:path*`,
  61. },
  62. ];
  63. }
  64. // 生产环境返回空数组,使用相对路径(由 Nginx 处理)
  65. return [];
  66. },
  67. images: {
  68. remotePatterns: [
  69. {
  70. protocol: "http",
  71. hostname: "192.168.124.9",
  72. port: backendPort,
  73. pathname: "/uploads/**",
  74. },
  75. {
  76. protocol: "http",
  77. hostname: "localhost",
  78. port: backendPort,
  79. pathname: "/uploads/**",
  80. },
  81. {
  82. protocol: "http",
  83. hostname: "127.0.0.1",
  84. port: backendPort,
  85. pathname: "/uploads/**",
  86. },
  87. ],
  88. },
  89. };
  90. export default nextConfig;