DEV Community

Joy Biswas
Joy Biswas

Posted on

I Self-Hosted n8n and Fixed the Websocket Headache

Getting Started with n8n on a VPS

I've heard a lot of good things about n8n, the open-source workflow automation tool. But I never got around to trying it until a few days ago. I borrowed a VPS from a friend and set it up. It was straightforward, but I ran into a couple of hurdles. In this post, I'll share my steps so you can avoid the same issues.

Of course I used Docker, which makes things smooth like butter. Another thing, you need an HTTPS connection for n8n to set up properly. Without it, you'll hit roadblocks.

Here's my docker-compose.yaml file if you want to try it. Just tweak it to fit your needs.

services: n8n: image: docker.n8n.io/n8nio/n8n:latest container_name: n8n restart: unless-stopped ports: - "5678:5678" environment: - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true - N8N_HOST=domain.com - N8N_PROTOCOL=https - N8N_BLOCK_ENV_ACCESS_IN_NODE=false - N8N_RUNNERS_ENABLED=true - GENERIC_TIMEZONE=Asia/Dhaka volumes: - n8n_data:/home/node/.n8n networks: - n8n-network volumes: n8n_data: networks: n8n-network: 
Enter fullscreen mode Exit fullscreen mode

I used Nginx as a reverse proxy. The biggest problem I faced was a websocket error. I also checked the network tab and there was just websocket errors. I kept getting "connection lost" message every time I tried to execute a workflow, no matter what environment variables I changed. I was stuck for hours. Then a friend suggested it might be an Nginx issue, and he was right. n8n relies on websocket connections, but my Nginx config wasn't set up to allow them.

image

I fixed it by adding these lines to my Nginx site config at /etc/nginx/sites-available/mydomain.com:

proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 
Enter fullscreen mode Exit fullscreen mode

Here's the full Nginx config file I ended up with. Remember to replace domain.com with your actual domain.

server { listen 443 ssl; server_name domain.com; access_log /var/log/nginx/n8n.access.log; error_log /var/log/nginx/n8n.error.log; ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/domain.com/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 location / { proxy_pass http://127.0.0.1:5678; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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 { if ($host = domain.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name domain.com; return 404; # managed by Certbot } 
Enter fullscreen mode Exit fullscreen mode

For SSL, I used Certbot to get a free certificate. First, set up an A record for your domain in your DNS settings. Then run this command:

sudo certbot --nginx -d domain.com 
Enter fullscreen mode Exit fullscreen mode

After that, visit your domain in a browser and finish the n8n setup. It should work smoothly now.

image

Overall, setting up n8n this way was worth it for custom automations. If you run into issues, double-check your websocket config in Nginx.

Top comments (0)