next.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { NextConfig } from "next";
  2. // 从环境变量读取后端端口,默认 8080(与后端 main.go 保持一致)
  3. // 如果设置了 NEXT_PUBLIC_BACKEND_PORT,优先使用(用于 Docker 部署等场景)
  4. const backendPort = process.env.NEXT_PUBLIC_BACKEND_PORT || "8080";
  5. const backendHost = process.env.NEXT_PUBLIC_BACKEND_HOST || "localhost";
  6. const nextConfig: NextConfig = {
  7. // 开发环境:代理 API 请求到后端
  8. // 生产环境:由 Nginx 处理,这个配置不会生效(因为生产环境是静态构建)
  9. async rewrites() {
  10. // 只在开发环境启用代理
  11. if (process.env.NODE_ENV === "development") {
  12. return [
  13. // 优先匹配后端 API 路径(这些需要代理到后端)
  14. {
  15. source: "/agent/profile/:path*",
  16. destination: `http://${backendHost}:${backendPort}/agent/profile/:path*`,
  17. },
  18. {
  19. source: "/agent/avatar/:path*",
  20. destination: `http://${backendHost}:${backendPort}/agent/avatar/:path*`,
  21. },
  22. {
  23. source: "/agent/embedding-config",
  24. destination: `http://${backendHost}:${backendPort}/agent/embedding-config`,
  25. },
  26. {
  27. source: "/agent/ai-config/:path*",
  28. destination: `http://${backendHost}:${backendPort}/agent/ai-config/:path*`,
  29. },
  30. // 匹配其他 API 路径(不以 /_next、/agent、/chat 开头的路径)
  31. // 例如:/login, /conversations, /messages 等
  32. {
  33. source: "/:path((?!_next|agent|chat|favicon.ico).*)",
  34. destination: `http://${backendHost}:${backendPort}/:path*`,
  35. },
  36. ];
  37. }
  38. // 生产环境返回空数组,使用相对路径(由 Nginx 处理)
  39. return [];
  40. },
  41. images: {
  42. remotePatterns: [
  43. {
  44. protocol: "http",
  45. hostname: "192.168.124.9",
  46. port: backendPort,
  47. pathname: "/uploads/**",
  48. },
  49. {
  50. protocol: "http",
  51. hostname: "localhost",
  52. port: backendPort,
  53. pathname: "/uploads/**",
  54. },
  55. {
  56. protocol: "http",
  57. hostname: "127.0.0.1",
  58. port: backendPort,
  59. pathname: "/uploads/**",
  60. },
  61. ],
  62. },
  63. };
  64. export default nextConfig;