Help n8n+docker+nginx+postgre Compose

Connection lost when accessing n8n editor behind NGINX (custom ports, restricted LAN)

Hi all,

We’re deploying n8n in an on-premise, air-gapped corporate network where the environment is not exposed to the public internet. Our setup uses NGINX as a reverse proxy (hosted on Windows), and n8n + PostgreSQL run inside Docker.


:puzzle_piece: Use Case Summary

  • Editor UI: served at https://n8n.example.com:5254
  • Webhooks: served externally at https://n8n.example.com:5465
  • Internal traffic (NGINX → Docker): proxied to http://127.0.0.1:5678

:backhand_index_pointing_right: This is a closed network setup — only specific internal users have access via DNS-resolved n8n.example.com.


:cross_mark: Problem

When accessing the editor at https://n8n.example.com:5254, everything loads except:

  • Editor constantly shows “Connection lost”
  • Saving or editing workflows becomes unstable
  • Browser dev tools show:
    GET /rest/push?pushRef=... → 500 (Internal Server Error)
  • n8n Docker logs include:
    ResponseError: Invalid origin!
    

:white_check_mark: Our Current Configuration

docker-compose.yml

services:
  postgres:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: strongpgpass
      POSTGRES_DB: n8ndb
    volumes:
      - postgres_data:/var/lib/postgresql/data

  n8n:
    image: n8nio/n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8ndb
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=strongpgpass

      - N8N_HOST=n8n.example.com
      - N8N_PORT=5678
      - N8N_PROTOCOL=https

      - N8N_EDITOR_BASE_URL=https://n8n.example.com:5254
      - WEBHOOK_URL=https://n8n.example.com:5465

      - N8N_PUSH_BACKEND=websocket
      - N8N_RUNNERS_ENABLED=true

volumes:
  postgres_data:

nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name n8n.example.com;
        return 301 https://$host$request_uri;
    }

    server {
        listen 5254 ssl;
        server_name n8n.example.com;

        ssl_certificate     C:/ssl/n8n.crt;
        ssl_certificate_key C:/ssl/n8n.key;

        location /rest/push {
            proxy_pass http://127.0.0.1:5678/rest/push;
            proxy_http_version 1.1;
            proxy_set_header Host n8n.example.com;
            proxy_set_header Origin https://n8n.example.com:5254;
            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;
        }

        location / {
            proxy_pass http://127.0.0.1:5678/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host n8n.example.com;
            proxy_set_header Origin https://n8n.example.com:5254;
            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;
        }
    }

    server {
        listen 5465 ssl;
        server_name n8n.example.com;

        ssl_certificate     C:/ssl/n8n.crt;
        ssl_certificate_key C:/ssl/n8n.key;

        location ~ ^/(webhook|webhook-test)/ {
            proxy_pass http://127.0.0.1:5678;
            proxy_http_version 1.1;
            proxy_set_header Host n8n.example.com;
            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;
        }

        location / {
            return 403;
        }
    }
}

:test_tube: Tried Already

  • Forwarding static Origin/Host headers in nginx
  • Ensuring N8N_EDITOR_BASE_URL matches exactly
  • Tested both server-sent-events and websocket
  • TLS is valid and working
  • Webhooks function correctly

:red_question_mark: Request for Help

What else might cause Invalid origin on /rest/push, even with all values matched?
We’re blocked from going live and would appreciate any guidance :folded_hands:

Thanks!

Information on your n8n setup

  • n8n version: 1.92.2
  • Database (default: SQLite): Postgre/Docker
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • **Operating system: Windows Server 2022 **
1 Like

I think it should be wss not websocket for that backend push env variable.
Also it is the default so you can just skip it.
Seems like something is not setup right with the wss so remove it and see if it helps.
If it doesn’t it might be nginx not setting wss correctly

1 Like

Also, aren’t you persisting your n8n folder? you may lose data if u don’t.

Also it looks like it’s the origin header or that custom port messing with the push connection. try removing the proxy_set_header Origin from nginx and also drop N8N_PUSH_BACKEND from the env, it’s default websocket now anyway. also probs best to avoid :5254 if you can and just use standard https on 443, keeps things clean and n8n won’t complain about origin mismatches.

I hope this helps

Thank you so much for taking the time to help me with my question about n8n. I truly appreciate the effort and thoughtful suggestions you provided. I’ll definitely be trying out the ideas you shared.

Wishing you all the best and continued success in your work!

Warm regards,
Batuhan

1 Like

@Batuhan_Polat

Thanks very much, feel free to reach out at any time, btw I would really appreciate it if you could quickly mark the best reply as a solution if okay.

Many thanks, and have a great day.

Samuel

I’m with the same issue, someone has a working Nginx template to share?

I’m agree with Samuel, 443 port on http2 is mandatory those days.