Websockets and SSE connection lost

Describe the problem/error/question

I get connection lost or connection error on scenario view or testing webhook

What is the error message (if any)?

console output

WebSocket connection to 'wss://n8n.cybershu.eu/rest/push?sessionId=gxaop3s3uuj' failed: 

Please share your workflow

I use docker-compose + nginx:
docker-compose

version: '3.1'

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=usr
      - N8N_BASIC_AUTH_PASSWORD=any_pass
      - N8N_HOST=n8n.cybershu.eu
      - N8N_PORT=5678
      - N8N_PUSH_BACKEND=websocket
      - N8N_PROTOCOL=https
      - WEBHOOK_URL=https://n8n.cybershu.eu/
      - VUE_APP_URL_BASE_API=https://n8n.cybershu.eu/
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./n8n:/home/node/.n8n
    command: "n8n start --tunnel"

nginx config

server {
    server_name n8n.cybershu.eu;

    location / {
        proxy_pass http://127.0.0.1:5678;
        proxy_buffering off;
        proxy_cache off;
        chunked_transfer_encoding off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

    location /rest/push {
	proxy_pass http://127.0.0.1:5678/rest/push;
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";
	proxy_set_header Host $host;
	proxy_redirect off;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;
    }

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/n8n.cybershu.eu/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/n8n.cybershu.eu/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = n8n.cybershu.eu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;
    server_name n8n.cybershu.eu;
    return 404; # managed by Certbot
}

I run uwsc ws://127.0.0.1:5678/rest/push but connection failed.

Information on your n8n setup

  • n8n version: newest
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker + nginx as reversed proxy
  • Operating system: ubuntu

Hi @michmzr, I am sorry you’re having trouble. Is your n8n instance running as it should? Any errors in the server logs?

And can you clarify if you’re seeing the same behaviour without the tunnel command (seeing you are already using your own domain n8n.cybershu.eu there should be no need for the tunnel)?

1 Like

yes, instance is running.
There is no warning or errors in dockers compose logs

n8n_1  | License manager not initialized
n8n_1  | Last session crashed
n8n_1  | n8n ready on 0.0.0.0, port 5678
n8n_1  | Initializing n8n process

I commented command with --tunnel, connection with websocket is still failing. I checked it on Brave, Safari and Chrome.

Hm, tbh I am not fully sure which impact the separate location directive for /rest/push has as I haven’t used nginx for quite a while. Are you also seeing problems when removing this directive temporarily?

Is the connection working without a reverse proxy (for example when testing an ssh tunnel)?

Any chance there are additional components sitting between your browser and n8n (something like Cloudflare perhaps)?

2 Likes

Are you also seeing problems when removing this directive temporarily?

No change

Is the connection working without a reverse proxy (for example when testing an ssh tunnel)?

No working

Any chance there are additional components sitting between your browser and n8n (something like Cloudflare perhaps)?

I removed config from Nginx and its started working, but without TLS and https

I removed config from Nginx and its started working, but without TLS and https

Hm, that’s strange and you might want to review your nginx config with the nginx community to be sure. It would however confirm this isn’t an n8n-specific problem but one on the reverse proxy level.

Based on my understanding there is no need for the additional location /rest/push directive to achieve a TLS/https connection from your browser. As a reference point, this (plus the certbot section added later) was my own nginx config when I was still using this web server.

2 Likes

@mutantx
location /rest/push was recommended by ChatGPT :wink:
Your config from GitHub - that-one-tom/n8n-on-oracle-vm: A step by step description of setting up n8n using Ubuntu, Docker, NGINX + UFW on an Oracle Cloud VM helped 🩷

My current config:

docker compose

version: '3.1'

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: unless-stopped
    container_name: n8n
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=...
      - N8N_BASIC_AUTH_PASSWORD=...
      - N8N_HOST=[server ip address]
      - N8N_PORT=5678
      - WEBHOOK_URL=https://my.sub.domain/
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./n8n:/home/node/.n8n

nginx config file

  GNU nano 4.8                                     /etc/nginx/sites-available/n8n.cybershu.eu
server {
    server_name [domain]; # domain like sub.example.com

    location / {
        proxy_pass http://127.0.0.1:5678; # ip address and port of dockerized n8n  
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
    }

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/[subdomain]/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[subdomain]/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = [subdomain]) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name [subdomain];
    return 404; # managed by Certbot
}
1 Like

Sweet, glad to hear this worked for you. Thanks a lot for confirming :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.