next.config.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // 匹配其他 API 路径(不以 /_next、/agent、/api、/chat 开头的路径)
  53. // /api/agent/prompts 由 app/api/agent/prompts/route.ts 代理,不在此转发
  54. {
  55. source: "/:path((?!_next|agent|api|chat|favicon.ico).*)",
  56. destination: `http://${backendHost}:${backendPort}/:path*`,
  57. },
  58. ];
  59. }
  60. // 生产环境返回空数组,使用相对路径(由 Nginx 处理)
  61. return [];
  62. },
  63. images: {
  64. remotePatterns: [
  65. {
  66. protocol: "http",
  67. hostname: "192.168.124.9",
  68. port: backendPort,
  69. pathname: "/uploads/**",
  70. },
  71. {
  72. protocol: "http",
  73. hostname: "localhost",
  74. port: backendPort,
  75. pathname: "/uploads/**",
  76. },
  77. {
  78. protocol: "http",
  79. hostname: "127.0.0.1",
  80. port: backendPort,
  81. pathname: "/uploads/**",
  82. },
  83. ],
  84. },
  85. };
  86. export default nextConfig;