Steps
Open the browser's Network tab
In the n8n editor press F12, open Network, filter on
pushand reload. The/rest/push?pushRef=…request is the live connection behind “Connection lost”. A healthy one shows status 101. A 500 or 401 means the proxy isn't passing the WebSocket upgrade; a 101 that closes at once usually means n8n refused the origin.Check the n8n log
Run
docker logs <container>(ordocker compose logs n8n). If you seeOrigin header does NOT match the expected originfollowed byInvalid origin!, n8n is rejecting the connection because it can't see your public host name. The log prints both values, so you can see which one is wrong.Forward the WebSocket upgrade
Nginx speaks HTTP/1.0 to the backend and drops
Upgradeunless you tell it otherwise. Addproxy_http_version 1.1;,proxy_set_header Upgrade $http_upgrade;andproxy_set_header Connection "upgrade";(or themapin the config below). Without them we got500 Internal Server Error, or401 Unauthorizedonce the host was passed, even with a valid login.Pass the public host, port included
Add
proxy_set_header Host $http_host;. With Nginx's default, n8n saw127.0.0.1:5801and refused the connection.$hostworks on port 443 but drops the port, so a site onhttps://n8n.example.com:8443was refused again;$http_hostkeeps it. PassingX-Forwarded-Hostworks too.Stop idle disconnects
Nginx closes a WebSocket that's quiet for longer than
proxy_read_timeout. With 10 seconds, ours was cut after exactly 10.0 s (code 1006). That's the moment the editor shows “Connection lost” and starts reconnecting. Setproxy_read_timeout 3600s;. Behind something that can't do WebSockets at all, setN8N_PUSH_BACKEND=sseon n8n: it uses a plain HTTP stream instead.
A working Nginx config
We ran this exact config (without the TLS lines): the live connection opened on the normal port and on :8443, a page from another site was still refused, and the editor, /healthz and the logged-in session all answered 200. Replace the server name and the port n8n listens on.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl;
server_name n8n.example.com;
# ssl_certificate ... ssl_certificate_key ...
location / {
proxy_pass http://127.0.0.1:5678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
}With Caddy, the short version is enough: Caddy passes the host and WebSocket upgrades on its own (we didn't run Caddy in this test).
n8n.example.com {
reverse_proxy 127.0.0.1:5678
}Then set WEBHOOK_URL=https://n8n.example.com/ on n8n so webhook URLs show your public address, as explained in n8n self-hosting errors.
What we tested, and what happened
On 19 October 2026 we ran n8n 2.43.7 (Node.js 24.22.0, SQLite) behind Nginx 1.26.3 and opened the editor's live connection (/rest/push, the one behind “Connection lost”) with a logged-in session cookie, sending the same Host and Origin headers a browser at https://n8n.example.com would. n8n ran with NODE_ENV=production, like the Docker image, unless noted. Each row changes one thing.
| Proxy setup | What happened | Fix |
|---|---|---|
Direct to n8n, browser-like Origin | Connected (101) and stayed open. | Baseline. |
Direct to n8n, no Origin header | Connected, then n8n sent Invalid origin! and closed with code 1008. | Normal: real browsers always send Origin. |
Bare proxy_pass only | HTTP 500 Internal Server Error. n8n log: Origin header does NOT match the expected origin, then ResponseError: Invalid origin! | Add the WebSocket headers and pass the host. |
Host passed, no Upgrade/Connection headers | HTTP 401 Unauthorized, although the login cookie was valid. | Add proxy_http_version 1.1, Upgrade and Connection. |
WebSocket headers, Host not passed | Connected, then Invalid origin!, code 1008. Log: Expected: "127.0.0.1:5801" | Add proxy_set_header Host $http_host; |
WebSocket headers + Host $host | Connected and stayed open; n8n pinged the connection. | Works on the standard port 443. |
Same, but site on a public port (:8443) | Invalid origin!: $host drops the port, so n8n expected n8n.example.com but the browser said n8n.example.com:8443. | Use $http_host, which keeps the port. |
Same with Host $http_host | Connected and stayed open. | Fixed. |
Only X-Forwarded-Host $host, no Host | Connected and stayed open. | n8n checks Forwarded, then X-Forwarded-Host, then Host. |
| Correct proxy, page opened from another site | Invalid origin! | That's the protection doing its job. |
Correct proxy, proxy_read_timeout 10s | Closed after exactly 10.0 s, code 1006. | Raise proxy_read_timeout. |
| Correct proxy, default timeout, editor heartbeat every 30 s | Stayed open for the full 150 s, with and without the heartbeat; n8n itself pinged every 60 s. | Fine with n8n alone, but n8n’s 60 s ping sits right at Nginx’s 60 s default, so give it more room. |
Same bad proxy (no Host), n8n started with npm (no NODE_ENV=production) | Connected and stayed open. | The origin check only runs in production mode, and the Docker image sets NODE_ENV=production. |
N8N_PUSH_BACKEND=sse, bare proxy_pass | HTTP 500, same Invalid origin! in the log. | Pass the host here too. |
N8N_PUSH_BACKEND=sse, Host passed, no WebSocket headers | 200, text/event-stream, first event :ok. | SSE needs no Upgrade headers; n8n sends X-Accel-Buffering: no so Nginx doesn't buffer it. |
Why it works on localhost but not behind the proxy
Before it accepts the live connection, n8n compares the browser's Origin (for example https://n8n.example.com) with the host the request was sent to. Behind a proxy that doesn't pass the host, n8n sees 127.0.0.1:5678 instead and refuses. The log line tells you which side is wrong: (Origin: "https://n8n.example.com" -> "n8n.example.com", Expected: "127.0.0.1:5801" -> "127.0.0.1:5801", Protocol: "https"). If “Expected” shows an internal address, pass Host; if it lacks the port the browser uses, use $http_host. This check only runs when NODE_ENV=production, which the official Docker image sets, so a test with npx n8n on your laptop can work while the same proxy in front of Docker fails.
Quick checklist
- 500 or 401 on
/rest/pushin the browser's Network tab: the proxy doesn't forward the WebSocket upgrade or the host. - “Invalid origin!” in the n8n log: pass
Host $http_host(orX-Forwarded-Host). - Works for a minute, then “Connection lost”: raise
proxy_read_timeouton Nginx, or the idle timeout of your load balancer or tunnel. - A proxy that can't do WebSockets: set
N8N_PUSH_BACKEND=sseon n8n and restart; it worked through Nginx without any Upgrade headers. - After any change, reload the proxy (
sudo nginx -t && sudo systemctl reload nginx) and hard-refresh the editor.
Good to know
- Payload too large, a vague 500 on big webhook bodies or a 413: n8n 413 Request Entity Too Large, payload limits tested.
- Login fails with a “secure cookie” message? That's HTTPS, not the proxy headers: see n8n won't start or you can't log in.
- Webhook gives 404 behind the proxy? Check test vs production URLs in n8n webhook not working.
What we didn't test
- A real browser, real TLS certificates, Caddy, Traefik, Apache, Cloudflare Tunnel and load balancers. We sent the same headers a browser sends; the TLS part was left out because it doesn't change these headers.
- Queue mode with several main instances and n8n Cloud.
Start from a working agent
FAQ
Why does n8n say "Connection lost" behind Nginx?
The editor's live connection to /rest/push is a WebSocket. If Nginx doesn't forward the Upgrade and Connection headers, or doesn't pass the public Host, n8n answers 500 or 401 or refuses it with "Invalid origin!", and the editor shows Connection lost. Add proxy_http_version 1.1, the Upgrade and Connection headers and proxy_set_header Host $http_host.
What does "Invalid origin!" mean in the n8n log?
n8n compared the browser's Origin header with the host it received and they didn't match, usually because the proxy sent 127.0.0.1:5678 as the host. Pass Host $http_host or X-Forwarded-Host from the proxy. The check only runs with NODE_ENV=production, which the Docker image sets.
Why does n8n keep disconnecting after a minute?
A proxy, load balancer or tunnel is closing the idle WebSocket. In Nginx raise proxy_read_timeout, for example to 3600s. In our test a 10-second timeout cut the connection after exactly 10 seconds.
Can n8n work without WebSockets?
Yes. Set N8N_PUSH_BACKEND=sse and restart n8n. The editor then uses Server-Sent Events over plain HTTP; through Nginx it only needed the Host header, no Upgrade headers.
Should I use $host or $http_host for n8n?
$http_host. It keeps the port, so it also works when n8n is published on a non-standard port such as 8443. With $host, n8n refused the connection on port 8443.
Related guides
- n8n won’t start or you can’t log in? 11 self-hosting errors, tested
- n8n HTTP Request node errors: 401, 404, ECONNREFUSED, SSL and 12 more, fixed
- n8n Redis node errors and hangs: connection, TLS and Set fixes, 50 tests
- n8n Wait node not resuming? Resume URL and timer fixes, 19 runs
- n8n MongoDB node errors: Authentication failed, Invalid JSON and query fixes, 40 runs
New guides
- GitHub release notifications in Slack (any public repo, stable releases only)
- Send RSS feeds to Slack with a keyword filter (free, incoming webhook)
- Forward emails to a Slack channel (Gmail, Outlook, any IMAP), free with an incoming webhook
- n8n Google Sheets Trigger not working: not firing, activation errors and fixes