Docker n8n on a subpath with nginx https "Connection lost"

Describe the problem/error/question

I have a docker n8n setup behind nginx on a subpath.

Here is my nginx config:

server {
    if ($host = mysite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name mysite.com;
    return 404;
}

server {
    listen 443 ssl;
    server_name mysite.com;

    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;

    root /mnt/chest/projects/mysite.com/html;
    index index.html;

    # Security headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;

    location /n8n/ {

        proxy_pass http://localhost:5678/;
        proxy_set_header Upgrade $http_upgrade; # Added for WebSocket
        proxy_set_header Connection “Upgrade”; # Modified for WebSocket
        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;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;

    }
}

UPD2: I have also tried to add this to no success:

location ~ ^/n8n/(webhook|webhook-test) {
        proxy_set_header Connection '';
        chunked_transfer_encoding off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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_set_header X-Frame-Options SAMEORIGIN;
        proxy_buffering off;
        proxy_cache off;
        proxy_pass http://localhost:5678/$1;
    }

Here is my docker run command:

sudo docker run --restart=always -d -it --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n -e N8N_SECURE_COOKIE="false" -e N8N_PATH="/n8n/" -e GENERIC_TIMEZONE="Europe/Moscow" -e TZ="Europe/Moscow" -e WEBHOOK_URL="https://mysite.com/n8n/" -e N8N_PROTOCOL="https" docker.n8n.io/n8nio/n8n

What is the error message (if any)?

I feel like it’s more than Telegram, but I only tried it for Telegram yet and I need it. So, for now, I add credentials (my bot token), test connection (get “Connection tested successfully”), then go to the workflow and press “Test workflow”. This gives me “Lost connection to the server”. Workflow doesn’t work.

Additionally, if I press F12 in my browser while on the workflow page and go to the Network tab, I see GET requests to wss://mysite.com/n8n/rest/push?pushRef= blocked with 401 Unauthorized error. I’m unsure if this relates to the issue, but it felt relevant.

UPD: I have “Connection lost” warning in the top right corner of any workflow, so it’s not about Telegram. I’ve also tried to connect to n8n directly, without nginx - and the warning was gone. So the problem is nginx, I need to configure something else for it to work. Any ideas?

Please share your workflow

Information on your n8n setup

  • n8n version: 1.82.2
  • Database (default: SQLite): didn’t change anything, so must be SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Unsure what it is, haven’t changed enything
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu Server

Hi,

Any reason why the env variable WEBHOOK_URL is between ‘’. Not sure but this might be the cause of the issue

Reg,
J.

This appeared to be a typo, but it doesn’t affect the situation. I’ve fixed it in the post and double checked it’s without quotes in the docker run command, thanks!

Tried to add start --tunnel at the end of my docker run - no effect

This is the closest solution so far:

    location /n8n/ {
#        allow 100.102.167.87;  # Tailscale network
#        deny all;
        proxy_pass http://localhost:5678/;
        proxy_set_header Upgrade $http_upgrade; # Added for WebSocket
        proxy_set_header Connection "Upgrade"; # Modified for WebSocket
        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;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
        proxy_http_version 1.1;
    }

And deleted regex location below.

This solves the problem with “Connection lost” message, but I’m still not able to receive webhooks, including the “on message” Telegram node (but also just Webhook node).

The problem with the “Connection lost” error was wrong quotes symbol in the proxy_set_header Connection “Upgrade”;
I got that solution from here somewhere, so be careful.

UPD: Okay, so, webhooks also work. The problem is that n8n doesn’t see the WEBHOOK_URL and N8N_HOST variables from docker run command and offers me some n8n.cloud URL. If I change n8n.cloud part to mysite.com/n8n - webhook works. It’s a temp solution for vanilla webhook, but won’t work for nodes that offer webhooks to some external services (like Telegram, yeah). At this point, I feel like this should be the answer to this question, but I’ll give myself a bit more time to maybe fix this, and then it will be completely solved for me.

UPD2: It was the --tunnel at the end of docker run. It makes n8n ignore webhook URL provided and use its own cloud thing. I also changed path, host and webhook URL a bit. That was a really frustrating journey, I hope this topic helps somebody in the future. Here is the final docker run command that worked for me:

sudo docker run --restart=always -d -it --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n -e N8N_SECURE_COOKIE="false" -e N8N_PATH="/n8n/" -e GENERIC_TIMEZONE="Europe/Moscow" -e TZ="Europe/Moscow" -e WEBHOOK_URL="https://mysite.com/n8n/" -e N8N_HOST="mysite.com" -e N8N_PROTOCOL="https" docker.n8n.io/n8nio/n8n start
2 Likes

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