How to Run n8n on Your VPS Server Using Docker and Nginx
Learn how to deploy n8n on your existing VPS without interfering with your primary website or application. This guide walks you through setting up Docker Compose and configuring Nginx to serve n8n on a subpath (e.g., yourdomain.com/n8n).
How to Run n8n on Your VPS Server Using Docker and Nginx
If you have a VPS server already running your website or another application and want to host n8n on it without interfering with the main service, you can do this using Docker and Nginx. This guide explains how to achieve that by deploying n8n on a subpath (e.g., yourdomain.com/n8n) instead of the root /.
Step 1: Create a Docker Compose File
Create a new docker-compose.yml file for your n8n service:
services:
n8n:
image: n8nio/n8n:latest
restart: always
ports:
- "5678:5678"
volumes:
- n8n_data:/home/node/.n8n
- ./local-files:/files
environment:
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=myStrongUser
- N8N_BASIC_AUTH_PASSWORD=myStrongPassword
- NODE_FUNCTION_ALLOW_EXTERNAL=*
- N8N_PATH=/n8n/
- N8N_RUNNERS_ENABLED=true
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
volumes:
n8n_data:
This configuration ensures:
Basic authentication for your n8n interface
Persistent storage for your workflows and credentials
Runs n8n on port 5678 internally
Run the container using:
docker compose up -d
Step 2: Configure Nginx as a Reverse Proxy
If your VPS already has Nginx serving your main website, you can add the following configuration to allow access to n8n on /n8n:
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name myDomain.com www.myDomain.com;
# Allow Certbot to verify the domain
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
try_files $uri =404;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name myDomain.com www.myDomain.com;
ssl_certificate /etc/letsencrypt/live/myDomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myDomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
client_max_body_size 10000M;
# React Frontend
location / {
root /home/myCodes/react2/dist;
index index.html;
try_files $uri /index.html;
}
# FastAPI backend
location /f_api/ {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# Flask backend
location /request/ {
proxy_pass http://localhost:5000;
include /etc/nginx/proxy_params;
proxy_redirect off;
proxy_buffering off;
}
# n8n interface with auth
location /n8n/ {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.n8n_auth;
proxy_pass http://127.0.0.1:5678/;
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-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Disable buffering for real-time updates
proxy_buffering off;
proxy_cache off;
# Increase timeouts for long-running operations
proxy_read_timeout 86400;
proxy_send_timeout 86400;
}
}
}
This setup proxies requests from https://yourdomain.com/n8n to your Docker container running n8n on port 5678.
Step 3: Restart Nginx
After editing your Nginx configuration, restart the service:
sudo nginx -t
sudo systemctl restart nginx
Step 4: Access n8n
Now you can access your n8n instance at:
https://yourdomain.com/n8n
Log in using the basic authentication credentials set in your docker-compose.yml.
With this setup, you’re successfully running n8n on your VPS without interfering with your existing website or apps.