// guides · n8n error index

n8n “Connection lost” behind a reverse proxy? 15 Nginx setups tested

Why the n8n editor shows “Connection lost” behind Nginx and how to fix it: missing WebSocket headers (500 or 401), the “Invalid origin!” host check, $host vs $http_host, idle timeouts and the SSE fallback. From 15 real runs on n8n 2.43.7.

Updated 2026-10-19 · about 10 minutes

Still stuck after these steps? Paste your exact n8n error and we'll reproduce it and email you the fix, free →

Steps

  1. Open the browser's Network tab

    In the n8n editor press F12, open Network, filter on push and 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.

  2. Check the n8n log

    Run docker logs <container> (or docker compose logs n8n). If you see Origin header does NOT match the expected origin followed by Invalid 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.

  3. Forward the WebSocket upgrade

    Nginx speaks HTTP/1.0 to the backend and drops Upgrade unless you tell it otherwise. Add proxy_http_version 1.1;, proxy_set_header Upgrade $http_upgrade; and proxy_set_header Connection "upgrade"; (or the map in the config below). Without them we got 500 Internal Server Error, or 401 Unauthorized once the host was passed, even with a valid login.

  4. Pass the public host, port included

    Add proxy_set_header Host $http_host;. With Nginx's default, n8n saw 127.0.0.1:5801 and refused the connection. $host works on port 443 but drops the port, so a site on https://n8n.example.com:8443 was refused again; $http_host keeps it. Passing X-Forwarded-Host works too.

  5. 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. Set proxy_read_timeout 3600s;. Behind something that can't do WebSockets at all, set N8N_PUSH_BACKEND=sse on 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 setupWhat happenedFix
Direct to n8n, browser-like OriginConnected (101) and stayed open.Baseline.
Direct to n8n, no Origin headerConnected, then n8n sent Invalid origin! and closed with code 1008.Normal: real browsers always send Origin.
Bare proxy_pass onlyHTTP 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 headersHTTP 401 Unauthorized, although the login cookie was valid.Add proxy_http_version 1.1, Upgrade and Connection.
WebSocket headers, Host not passedConnected, then Invalid origin!, code 1008. Log: Expected: "127.0.0.1:5801"Add proxy_set_header Host $http_host;
WebSocket headers + Host $hostConnected 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_hostConnected and stayed open.Fixed.
Only X-Forwarded-Host $host, no HostConnected and stayed open.n8n checks Forwarded, then X-Forwarded-Host, then Host.
Correct proxy, page opened from another siteInvalid origin!That's the protection doing its job.
Correct proxy, proxy_read_timeout 10sClosed after exactly 10.0 s, code 1006.Raise proxy_read_timeout.
Correct proxy, default timeout, editor heartbeat every 30 sStayed 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_passHTTP 500, same Invalid origin! in the log.Pass the host here too.
N8N_PUSH_BACKEND=sse, Host passed, no WebSocket headers200, 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

Good to know

What we didn't test

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

New guides

Still stuck on your n8n error?

Paste the exact error and say what the workflow should do. We reproduce it on a real n8n install and email you the fix or a working workflow, usually within 24h. Free while we're in beta.

We'll only email you about your error and TaskToAgent.