DEV Community

Cover image for Fix n8n "Connection lost" behind a reverse proxy
Fernando Chagas for Nuvits

Posted on Originally published at nuvits.net

Fix n8n "Connection lost" behind a reverse proxy

The n8n editor opens, the workflows are there, but the top right corner says "Connection lost". The warning means the editor lost its WebSocket at /rest/push, the channel the server uses to send events to the screen, and the one dropping that connection is almost always the reverse proxy in front of n8n. This guide shows the two causes we reproduced, how to tell which one you have, and the configuration that worked on Nginx, Caddy and Traefik.

The test was run on December 6, 2026 with n8n 3.4.4 (docker.n8n.io/n8nio/n8n:3.4.4) in a clean Ubuntu 24.04.5 LTS container with Docker 29.10.1. In front of n8n, one at a time: Nginx 1.30.8, Caddy 2.11.9 and Traefik v3.8.0. The editor was opened in Chrome, with the console read through DevTools.

n8n 3.4.4 editor with a workflow open and the Connection lost warning with an orange triangle in the top right corner, where the Publish button normally is

The two causes

On n8n 3.4.4, GET /rest/settings returns pushBackend set to websocket. The editor opens wss://n8n.example.com/rest/push?pushRef=..., and the server only accepts the connection when both conditions below hold.

1. The proxy passes the WebSocket upgrade through. Nginx does not forward the Upgrade and Connection headers unless you tell it to. With a location that only has proxy_pass and the X-Forwarded-* headers, the request reached n8n as a plain GET, and n8n answered 401 Unauthorized. In the Chrome console, the editor retried after 1, 2, 4 and 8 seconds. In this case the corner warning did not show up during the 30-second test, because the editor spent the whole time between attempts; the clear signal was the console:

[WebSocketClient] Connection error: Event
[WebSocketClient] Connection lost, code=1006
[WebSocketClient] Attempting to reconnect in 1000ms
Enter fullscreen mode Exit fullscreen mode

2. The browser's Origin matches the host n8n receives. n8n compares the Origin header with X-Forwarded-Host (or Forwarded) and, without them, with Host. By default, Nginx sends the proxy_pass address as Host. With the upgrade right but without proxy_set_header Host $host, the connection opened, n8n sent Invalid origin! and closed it with code 1008. That is the case in the image above. The n8n log records the mismatch:

Origin header does NOT match the expected origin. (Origin: "https://n8n.example.com" -> "n8n.example.com", Expected: "n8n:5678" -> "n8n:5678", Protocol: "https")
Enter fullscreen mode Exit fullscreen mode

In the Chrome console:

[WebSocketClient] Connection lost, code=1008
Enter fullscreen mode Exit fullscreen mode

N8N_HOST is not part of that comparison. Changing N8N_HOST, N8N_WEBHOOK_URL or N8N_PROXY_HOPS does not fix "Connection lost".

How to confirm the cause

Start with the browser console (F12, Console tab) while the editor is open: repeated code=1006 points to a blocked upgrade; code=1008 points to the Origin check. Then the n8n log:

docker compose logs n8n | grep "Origin header does NOT match"
Enter fullscreen mode Exit fullscreen mode

A line here means cause 2. Without signing in, you can test the upgrade from the VPS or from your computer:

curl --http1.1 -sS -o /dev/null -w '%{http_code}\n' --max-time 3 \
  -H 'Connection: Upgrade' -H 'Upgrade: websocket' \
  -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' \
  "https://n8n.example.com/rest/push?pushRef=test"
Enter fullscreen mode Exit fullscreen mode

Replace n8n.example.com with your domain. --http1.1 is required: without it, curl negotiates HTTP/2 with proxies that support it, such as Caddy and Traefik, and no upgrade happens. In the test, the proxy that blocked the upgrade answered 401. The proxies that passed it answered 101 (and curl ends with Operation timed out, which is expected, because the connection stays open).

Nginx

This server block passed both tests: 101 from curl and the editor connected, with no "Connection lost", for almost 3 minutes.

/etc/nginx/conf.d/n8n.conf

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 443 ssl;
    server_name n8n.example.com;
    ssl_certificate     /etc/nginx/certs/n8n.crt;
    ssl_certificate_key /etc/nginx/certs/n8n.key;

    location / {
        proxy_pass http://n8n:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • proxy_http_version 1.1, Upgrade and Connection fix cause 1. The map sends close when the request is not an upgrade.
  • proxy_set_header Host $host fixes cause 2.
  • proxy_pass http://n8n:5678 is for Nginx running in a container on the same network as n8n. With Nginx installed on the host, use http://127.0.0.1:5678 and publish the port on loopback only ("127.0.0.1:5678:5678").

After editing, check and reload. With Nginx in a container, in the same Compose project as n8n:

docker compose exec nginx nginx -t
docker compose exec nginx nginx -s reload
Enter fullscreen mode Exit fullscreen mode

With Nginx installed on the host, the equivalent is sudo nginx -t && sudo systemctl reload nginx.

Do not set proxy_read_timeout below 60 seconds. n8n sends a WebSocket ping every 60 seconds. With the Nginx default (60 s), the connection stayed open for the almost 3 minutes of the test. With proxy_read_timeout 30s, Nginx closed the connection at 30 seconds.

Caddy

Caddy's reverse_proxy passes WebSockets through with no extra settings and sends X-Forwarded-Host with the original domain, which is what n8n compares with Origin. This Caddyfile returned 101 and kept the editor connected:

Caddyfile

{$N8N_DOMAIN} {
    reverse_proxy n8n:5678
}
Enter fullscreen mode Exit fullscreen mode

If "Connection lost" shows up with Caddy, look for another proxy along the way, such as a CDN or a second proxy, and repeat the curl test against each one. For 502 or certificate errors in Caddy, see Fix Caddy 502 Bad Gateway and HTTPS errors with Docker.

Traefik

Traefik also passes WebSockets through by default and keeps the request's original host. With the Docker provider, these labels on the n8n service returned 101:

docker-compose.yml

services:
  n8n:
    labels:
      - traefik.enable=true
      - traefik.http.routers.n8n.rule=Host(`n8n.example.com`)
      - traefik.http.routers.n8n.entrypoints=websecure
      - traefik.http.routers.n8n.tls=true
      - traefik.http.services.n8n.loadbalancer.server.port=5678
Enter fullscreen mode Exit fullscreen mode

In the test, Traefik used its default certificate, with no ACME resolver. In production, the tls.certresolver label points to your Traefik resolver. Choosing between the two proxies is covered in Traefik vs Caddy on a Docker VPS.

What about N8N_PROXY_HOPS and WEBHOOK_URL?

Both variables show up in every guide about n8n behind a proxy, but they deal with something else. In the test, neither one changed the WebSocket result.

  • N8N_PROXY_HOPS=1 makes n8n trust the X-Forwarded-For header from one proxy. With 0, the log showed ValidationError: The 'X-Forwarded-For' header is set but the Express 'trust proxy' setting is false (default).. The message itself warns that the rate limiter then cannot identify each user. With two proxies in a row, the value is 2.
  • N8N_WEBHOOK_URL sets the public address of webhooks. Without it, the urlBaseWebhook of n8n 3.4.4 was https://n8n.example.com:5678/, with the internal port. With N8N_WEBHOOK_URL=https://n8n.example.com/, it was https://n8n.example.com/. The old name, WEBHOOK_URL, still works on 3.4.4, but startup warns WEBHOOK_URL -> Use N8N_WEBHOOK_URL instead.

The n8n environment block used in every test:

docker-compose.yml

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n:3.4.4
    environment:
      - N8N_HOST=${N8N_DOMAIN}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - N8N_WEBHOOK_URL=https://${N8N_DOMAIN}/
      - N8N_PROXY_HOPS=1
Enter fullscreen mode Exit fullscreen mode

The webhook showing localhost:5678 is covered in The n8n webhook shows localhost:5678. The full installation, with PostgreSQL, the runner and Caddy, is in How to install n8n with Docker on a VPS, and the machine to run n8n on is at n8n VPS.

What the test did not cover

n8n ran with SQLite, without an external runner and without public certificates: Caddy used tls internal, Nginx a self-signed certificate and Traefik its default certificate. We did not test a CDN in front of the proxy, Nginx Proxy Manager, or the SSE push backend (N8N_PUSH_BACKEND=sse).


Originally published at https://nuvits.net/en/blog/fix-n8n-connection-lost-reverse-proxy

Top comments (1)

Collapse
 
opsforge profile image
Youngchan Kim •

Two more cases that look identical from the browser, in case they save someone an afternoon:

  • More than one n8n behind the proxy (queue mode with several mains, or a rolling deploy). The push channel is per instance, so the editor can upgrade fine and still flap: the socket lands on instance A while execution events come from instance B. Sticky routing on the editor session (cookie or ip_hash) hides it, and it returns every time an instance restarts, which is why it reads as random. Workers do not serve the editor; only mains do.
  • Proxy-level auth in front of /rest/push. A WebSocket handshake cannot answer a 401 WWW-Authenticate: Basic the way a page load can, so basic auth wrapped around the whole location / gives the same 1/2/4/8 second retry loop as cause 1, but the response body is an HTML 401 instead of n8n's JSON. curl -i on the upgrade test tells the two apart; the status code alone does not.

And when a CDN sits in front of the proxy, the CDN's idle WebSocket timeout is what closes the socket, not Nginx. Cloudflare drops idle WebSockets at around 100 seconds, so n8n's 60 second ping has to survive the whole chain: keep proxy_read_timeout above the CDN's number, and run the curl test with --resolve straight at the origin to see which hop is answering.

One addition on probing: /healthz returning 200 says nothing about push. Two separate checks, HTTP 200 on healthz and HTTP 101 on /rest/push with the upgrade headers, catch the difference before a user does.