somratpro commited on
Commit
caf8b68
·
1 Parent(s): 3e988bf

refactor: configure n8n to use /app/ subpath natively and remove manual path stripping from health-server proxy

Browse files
Files changed (2) hide show
  1. health-server.js +4 -19
  2. start.sh +6 -4
health-server.js CHANGED
@@ -313,22 +313,15 @@ const server = http.createServer(async (req, res) => {
313
  return res.end();
314
  }
315
 
316
- // 3. Proxy to n8n (strip /app prefix)
317
- let proxyPath = pathname.substring(APP_BASE.length);
318
- if (!proxyPath.startsWith("/")) proxyPath = "/" + proxyPath;
319
-
320
- // Handle n8n's common 404 on root / by redirecting to workflows
321
- if (proxyPath === "/" && req.method === "GET") {
322
- res.writeHead(302, { Location: APP_BASE + "/home/workflows" });
323
- return res.end();
324
- }
325
 
326
  const proxyHeaders = {
327
  ...req.headers,
328
  host: `127.0.0.1:${TARGET_PORT}`,
329
  "x-forwarded-for": req.socket.remoteAddress,
 
330
  "x-forwarded-proto": "https",
331
- "x-forwarded-prefix": APP_BASE,
332
  };
333
 
334
  const proxyReq = http.request(
@@ -340,10 +333,6 @@ const server = http.createServer(async (req, res) => {
340
  headers: proxyHeaders,
341
  },
342
  (proxyRes) => {
343
- // Rewrite Location header for redirects
344
- if (proxyRes.headers.location && proxyRes.headers.location.startsWith("/")) {
345
- proxyRes.headers.location = APP_BASE + proxyRes.headers.location;
346
- }
347
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
348
  proxyRes.pipe(res);
349
  },
@@ -364,11 +353,7 @@ const server = http.createServer(async (req, res) => {
364
 
365
  server.on("upgrade", (req, socket, head) => {
366
  const url = parseRequestUrl(req.url);
367
- let proxyPath = url.pathname;
368
- if (proxyPath.startsWith(APP_BASE)) {
369
- proxyPath = proxyPath.substring(APP_BASE.length);
370
- }
371
- if (!proxyPath.startsWith("/")) proxyPath = "/" + proxyPath;
372
 
373
  const proxySocket = net.connect(TARGET_PORT, TARGET_HOST, () => {
374
  proxySocket.write(
 
313
  return res.end();
314
  }
315
 
316
+ // Proxy to n8n (Pass full path as n8n is configured with N8N_PATH=/app/)
317
+ const proxyPath = pathname;
 
 
 
 
 
 
 
318
 
319
  const proxyHeaders = {
320
  ...req.headers,
321
  host: `127.0.0.1:${TARGET_PORT}`,
322
  "x-forwarded-for": req.socket.remoteAddress,
323
+ "x-forwarded-host": req.headers.host,
324
  "x-forwarded-proto": "https",
 
325
  };
326
 
327
  const proxyReq = http.request(
 
333
  headers: proxyHeaders,
334
  },
335
  (proxyRes) => {
 
 
 
 
336
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
337
  proxyRes.pipe(res);
338
  },
 
353
 
354
  server.on("upgrade", (req, socket, head) => {
355
  const url = parseRequestUrl(req.url);
356
+ const proxyPath = url.pathname;
 
 
 
 
357
 
358
  const proxySocket = net.connect(TARGET_PORT, TARGET_HOST, () => {
359
  proxySocket.write(
start.sh CHANGED
@@ -15,10 +15,12 @@ mkdir -p "$N8N_HOME"
15
  SPACE_HOST_DETECTED="${SPACE_HOST_OVERRIDE:-${SPACE_HOST:-}}"
16
  if [ -n "$SPACE_HOST_DETECTED" ]; then
17
  export N8N_HOST="${N8N_HOST:-$SPACE_HOST_DETECTED}"
18
- # n8n runs at / internally; proxy handles /app prefix stripping
19
- export N8N_PATH="/"
20
- export WEBHOOK_URL="${WEBHOOK_URL:-https://${SPACE_HOST_DETECTED}/app/}"
21
- export N8N_EDITOR_BASE_URL="${N8N_EDITOR_BASE_URL:-https://${SPACE_HOST_DETECTED}/app/}"
 
 
22
  fi
23
 
24
  export N8N_PORT
 
15
  SPACE_HOST_DETECTED="${SPACE_HOST_OVERRIDE:-${SPACE_HOST:-}}"
16
  if [ -n "$SPACE_HOST_DETECTED" ]; then
17
  export N8N_HOST="${N8N_HOST:-$SPACE_HOST_DETECTED}"
18
+ # Official n8n subpath configuration
19
+ export N8N_PATH="/app/"
20
+ export N8N_PROTOCOL="https"
21
+ export N8N_HOST="${SPACE_HOST_DETECTED}"
22
+ export WEBHOOK_URL="https://${SPACE_HOST_DETECTED}/app/"
23
+ export N8N_EDITOR_BASE_URL="https://${SPACE_HOST_DETECTED}/app/"
24
  fi
25
 
26
  export N8N_PORT