fix: api-proxy per middleware (laufzeit-env statt build-time rewrites)
This commit is contained in:
1 parent
4b915d3b52
commit
df654fcb92
2 files changed
+17
No files matched your search
@@ -0,0 +1,15 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
|
||||
// Proxies /api/* to the backend. Runs per request, so BACKEND_URL is read
|
||||
// at RUNTIME (unlike next.config.js rewrites, which are baked at build time
|
||||
// in standalone mode and ignore container env vars).
|
||||
export function middleware(req: NextRequest) {
|
||||
const backend = process.env.BACKEND_URL || "http://127.0.0.1:8000";
|
||||
const target = new URL(req.nextUrl.pathname + req.nextUrl.search, backend);
|
||||
return NextResponse.rewrite(target);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: "/api/:path*",
|
||||
};
|
||||
@@ -9,6 +9,8 @@ const nextConfig = {
|
||||
],
|
||||
},
|
||||
async rewrites() {
|
||||
// Dev fallback only. In Docker/standalone the middleware.ts proxy handles
|
||||
// /api/* with the runtime BACKEND_URL (rewrites are build-time there).
|
||||
const backend = process.env.BACKEND_URL || "http://127.0.0.1:8000";
|
||||
return [
|
||||
{ source: "/api/:path*", destination: `${backend}/api/:path*` },
|
||||
|
||||
Reference in new issue
Block a user