Self-hosting n8n is one of the best choices a business can make for automation. It gives you full control over your workflows, eliminates per-execution SaaS pricing, and keeps sensitive API credentials stored securely on your own infrastructure.
However, many engineers deploy n8n by spinning up a VPS, launching n8n on port 5678, and pointing a DNS record straight to it.
Exposing an unhedged n8n instance directly to the public internet without proper proxying, firewall constraints, or hardened authentication turns your automation server into an easy target. Webhooks become vulnerable to scanning, admin interfaces face brute-force attacks, and unencrypted traffic leaves payloads exposed.
Control without security is just exposure. Here is how to deploy n8n securely in a production environment step by step.
Step 1: Isolate n8n Using Docker Compose
Never run n8n directly on the host interface or bind port 5678 to 0.0.0.0. Instead, run n8n inside a Docker container on an isolated internal network. This ensures n8n is accessible only to your internal reverse proxy.
Here is a hardened docker-compose.yml setup using PostgreSQL as the backend:
version: '3.8'
networks:
n8n_internal:
driver: bridge
services:
postgres:
image: postgres:15-alpine
restart: always
environment:
POSTGRES_USER: n8n_admin
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: n8n_prod
volumes:
- postgres_storage:/var/lib/postgresql/data
networks:
- n8n_internal
n8n:
image: docker.n8n.io/n8nio/n8n:latest
restart: always
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n_prod
- DB_POSTGRESDB_USER=n8n_admin
- DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
- N8N_HOST=n8n.yourdomain.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- NODE_ENV=production
- WEBHOOK_URL=https://n8n.yourdomain.com/
volumes:
- n8n_storage:/home/node/.n8n
networks:
- n8n_internal
volumes:
postgres_storage:
n8n_storage:
Notice that neither service publishes host ports directly. Traffic cannot reach n8n unless routed through a proxy container attached to n8n_internal.
Step 2: Put n8n Behind a Reverse Proxy
Place a reverse proxy like Caddy or Nginx in front of n8n to handle SSL termination, security headers, and request forwarding. Using Caddy simplifies certificate management while keeping configurations minimal.
Here is an example Caddyfile configuration:
n8n.yourdomain.com {
encode gzip
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "SAMEORIGIN"
Referrer-Policy "no-referrer-when-downgrade"
}
reverse_proxy n8n:5678 {
header_up X-Forwarded-Host {host}
header_up X-Real-IP {remote_host}
}
}
This layout enforces HTTPS, sets defense-in-depth HTTP headers, and prevents raw Node.js endpoints from accepting direct public traffic.
Step 3: Harden the Host Network
Lock down the host server's networking using Uncomplicated Firewall (UFW) so only essential ports remain open:
# Set default policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow standard web traffic and SSH
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Enable firewall
sudo ufw enable
Additionally, install and configure fail2ban to monitor your web server access logs. It automatically updates firewall rules to block IP addresses displaying malicious brute-force authentication behavior.
Step 4: Automate Database Backups
Security includes disaster recovery. Ensure your database state is regularly backed up and offloaded off-site rather than relying on manual snapshotting.
A lightweight cron script can dump your database state daily:
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/var/backups/n8n"
mkdir -p $BACKUP_DIR
# Dump PostgreSQL data from the running container
docker exec -t $(docker ps -qf "name=postgres") pg_dump -U n8n_admin n8n_prod > $BACKUP_DIR/n8n_backup_$TIMESTAMP.sql
# Retain backups for 7 days
find $BACKUP_DIR -type f -mtime +7 -name "*.sql" -delete
Sync these output files to remote object storage using a CLI utility like aws-cli or rclone.
Conclusion
Self-hosting n8n provides complete ownership over business processes and integration infrastructure. However, taking ownership of the software means taking ownership of its operational security. By isolating containers, enforcing TLS behind a reverse proxy, restricting open host ports, and automating data backups, you ensure your automation engine remains secure and resilient.
About me
Tanmay builds AI agents and automation for businesses and is open to freelance projects. Feel free to connect with Tanmay on LinkedIn to discuss custom automation setups, DevOps implementations, or backend architecture.
Top comments (0)