refactor: strip app path prefix in health-server proxy and set n8n root path to facilitate subpath hosting
Browse files- health-server.js +12 -3
- start.sh +2 -2
health-server.js
CHANGED
|
@@ -313,8 +313,9 @@ const server = http.createServer(async (req, res) => {
|
|
| 313 |
return res.end();
|
| 314 |
}
|
| 315 |
|
| 316 |
-
// Proxy to n8n (
|
| 317 |
-
|
|
|
|
| 318 |
|
| 319 |
const proxyHeaders = {
|
| 320 |
...req.headers,
|
|
@@ -333,6 +334,10 @@ const server = http.createServer(async (req, res) => {
|
|
| 333 |
headers: proxyHeaders,
|
| 334 |
},
|
| 335 |
(proxyRes) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
| 337 |
proxyRes.pipe(res);
|
| 338 |
},
|
|
@@ -353,7 +358,11 @@ const server = http.createServer(async (req, res) => {
|
|
| 353 |
|
| 354 |
server.on("upgrade", (req, socket, head) => {
|
| 355 |
const url = parseRequestUrl(req.url);
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
const proxySocket = net.connect(TARGET_PORT, TARGET_HOST, () => {
|
| 359 |
proxySocket.write(
|
|
|
|
| 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 |
const proxyHeaders = {
|
| 321 |
...req.headers,
|
|
|
|
| 334 |
headers: proxyHeaders,
|
| 335 |
},
|
| 336 |
(proxyRes) => {
|
| 337 |
+
// Rewrite Location header for redirects
|
| 338 |
+
if (proxyRes.headers.location && proxyRes.headers.location.startsWith("/")) {
|
| 339 |
+
proxyRes.headers.location = APP_BASE + proxyRes.headers.location;
|
| 340 |
+
}
|
| 341 |
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
| 342 |
proxyRes.pipe(res);
|
| 343 |
},
|
|
|
|
| 358 |
|
| 359 |
server.on("upgrade", (req, socket, head) => {
|
| 360 |
const url = parseRequestUrl(req.url);
|
| 361 |
+
let proxyPath = url.pathname;
|
| 362 |
+
if (proxyPath.startsWith(APP_BASE)) {
|
| 363 |
+
proxyPath = proxyPath.substring(APP_BASE.length);
|
| 364 |
+
}
|
| 365 |
+
if (!proxyPath.startsWith("/")) proxyPath = "/" + proxyPath;
|
| 366 |
|
| 367 |
const proxySocket = net.connect(TARGET_PORT, TARGET_HOST, () => {
|
| 368 |
proxySocket.write(
|
start.sh
CHANGED
|
@@ -15,8 +15,8 @@ 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 |
-
#
|
| 19 |
-
export N8N_PATH="/
|
| 20 |
export N8N_PROTOCOL="https"
|
| 21 |
export N8N_HOST="${SPACE_HOST_DETECTED}"
|
| 22 |
export WEBHOOK_URL="https://${SPACE_HOST_DETECTED}/app/"
|
|
|
|
| 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 |
+
# Internal root hosting with external subpath awareness
|
| 19 |
+
export N8N_PATH="/"
|
| 20 |
export N8N_PROTOCOL="https"
|
| 21 |
export N8N_HOST="${SPACE_HOST_DETECTED}"
|
| 22 |
export WEBHOOK_URL="https://${SPACE_HOST_DETECTED}/app/"
|