我的 n8n 架在 AWS EC2 裡、用 docker 啟動,接在cloudfront distribution 跟 ALB 後面。cloudfront 對外使用 redirect HTTP to HTTPS、origin 是使用 HTTP 連 ALB。ALB 的 listener 是 HTTP、target group 也是用 HTTP 連到 target。
n8n 升級到 v1.86 後 editor 右上角會出現 Connection lost 的警告:

用瀏覽器的 devtool 看是 web socket wss://
連線有問題,會想換 protocol 、往 http://
走。
從這篇看起來可以更改 push backend 為 sse
,就可以不使用 web socket。對應的 environment variable (ref):
N8N_PUSH_BACKEND=sse
設定 push backend 為 sse
後用 devtool 看沒有對 web socket 的連線,但依然 Connection lost。
發現 n8n 的 log 有這段:
n8n-1 | Origin header is missing
n8n-1 | ResponseError: Invalid origin!
n8n-1 | at Push.handleRequest (/usr/local/lib/node_modules/n8n/src/push/index.ts:143:10)
n8n-1 | at /usr/local/lib/node_modules/n8n/src/push/index.ts:100:10
n8n-1 | at Layer.handleRequest (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/router@2.2.0/node_modules/router/lib/layer.js:152:17)
n8n-1 | at trimPrefix (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/router@2.2.0/node_modules/router/index.js:342:13)
n8n-1 | at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/router@2.2.0/node_modules/router/index.js:297:9
n8n-1 | at processParams (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/router@2.2.0/node_modules/router/index.js:582:12)
n8n-1 | at next (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/router@2.2.0/node_modules/router/index.js:291:5)
n8n-1 | at /usr/local/lib/node_modules/n8n/src/auth/auth.service.ts:110:18
n8n-1 | at processTicksAndRejections (node:internal/process/task_queues:105:5)
看來是 cloudfront 往 origin 這段送的 request 缺少 origin header,參考這篇後我直接在 cloudfront distribution 的 origin 設定加 header origin
、value 寫 n8n.mydomain.org:[ALB_LISTENER_PORT]
。

解決啦!!🎉🎉🎉
在 cloudfront distribution 設好 header 後,即使把 push backend 改回 web socket 也能正常運作!🥳
本文同步發表於 https://www.cjwind.cc/n8n-editor-connection-lost-after-v1-86/