A typical first self-hosted n8n looks like this. There is one instance. The editor is open to the internet. The database is SQLite on a Docker volume, the image tag is latest, and workflows are changed directly where they run. That works until someone saves a half-finished change to a live workflow, or an image update changes a setting overnight.
This is how we run n8n for ourselves and for clients. There are two separate instances, stage and production, and each has its own database. The editor sits behind Cloudflare Access. Nothing listens on a public port. Every version is pinned, and a change reaches production only after it has run on stage.
Two environments, two compose files
Stage and production are two Docker Compose projects with the same services: PostgreSQL, n8n, a container for Code node runners, and cloudflared. Each one has its own project name, volumes, database and Cloudflare tunnel. They share nothing at runtime, so breaking stage cannot touch production.
Both run from one Git repository. Production runs from a working copy on the main branch, stage from a second working copy on the stage branch (git worktree makes this cheap). Each has its own file, compose.prod.yml or compose.stage.yml, and every command names it with -f.
There is deliberately no docker-compose.yml. Without -f, docker compose up finds no file and does nothing. That is inconvenient on purpose: nobody can recreate production from the stage working copy by typing a command in the wrong directory.
Here is the production file, shortened:
name: n8n-prod # stage uses its own name: n8n-stage
services:
postgres:
image: postgres:18.6-alpine
restart: unless-stopped
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
N8N_DB_PASSWORD: ${N8N_DB_PASSWORD} # init script creates the n8n role and database
volumes:
- pg_data:/var/lib/postgresql # Postgres 18: the volume goes here, not to .../data
- ./postgres/init.sh:/docker-entrypoint-initdb.d/init.sh:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
n8n:
image: docker.n8n.io/n8nio/n8n:2.48.7
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: ${N8N_DB_PASSWORD}
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
N8N_HOST: n8n.example.com
N8N_PROTOCOL: https
N8N_WEBHOOK_URL: https://n8n.example.com/
N8N_PROXY_HOPS: "1" # exactly one proxy in front: cloudflared
N8N_RUNNERS_MODE: external # Code nodes run in the n8n-runners container
N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
ports:
- "127.0.0.1:5778:5678" # local only; everyone else comes through the tunnel
volumes:
- n8n_data:/home/node/.n8n
n8n-runners:
image: n8nio/runners:2.48.7 # always the same version as n8n
restart: unless-stopped
environment:
N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
cloudflared:
image: cloudflare/cloudflared:2026.10.3
restart: unless-stopped
command: tunnel --no-autoupdate run
environment:
TUNNEL_TOKEN: ${CLOUDFLARE_TUNNEL_TOKEN}
volumes:
pg_data:
n8n_data:
Three of these settings matter more than they look:
N8N_WEBHOOK_URL. Without it, n8n builds webhook URLs from its own host and port, and external services get an address with:5678that they cannot reach. Older guides useWEBHOOK_URL, which is deprecated in n8n 2.x.N8N_PROXY_HOPS: "1". There is exactly one proxy in front of n8n, so n8n can trust the forwarded headers for the client address and protocol.N8N_RUNNERS_MODE: external. Code nodes (JavaScript and Python) run in a separaten8nio/runnerscontainer that has no access to the n8n database or volumes.
PostgreSQL instead of SQLite
SQLite is the default, and it is fine for trying n8n. For anything a business depends on, we use PostgreSQL. pg_dump takes a consistent backup while n8n keeps running, while with SQLite you copy a file that may be in the middle of a write. n8n gets its own role that owns only its own database. PostgreSQL is also the base for queue mode with Redis and separate workers, which we add when one n8n process is no longer enough.
There are two pitfalls in the file above. First, Postgres 18 images expect the volume at /var/lib/postgresql, not at /var/lib/postgresql/data like older guides say. Second, n8n waits for the pg_isready healthcheck, so after a reboot it does not start before the database accepts connections.
No public ports: Cloudflare Tunnel
cloudflared opens an outbound connection to Cloudflare and receives the hostname’s traffic through it. The server needs no inbound ports, no public IP and no certificate management, because TLS ends at Cloudflare. n8n itself is bound to 127.0.0.1.
We keep the tunnels, their routes and the DNS records in Terraform. The route list ends with a catch-all http_status:404, so a hostname nobody configured never reaches a service. The tunnel resource has prevent_destroy, because a recreated tunnel gets a new token and every cloudflared using the old one stops connecting. And the DNS record that publishes a hostname depends on its Access application, so the address goes live only after the login is in front of it.
Cloudflare Access in front of the editor
The whole hostname is behind a Cloudflare Access application. Before the n8n login page appears, Cloudflare asks for an email address and sends a one-time code. Only addresses on a short list get a code, and the session lasts 24 hours. After that comes the normal n8n login, so an attacker has to get past two different systems.
Webhooks are the exception. Telegram or a payment provider cannot log in, and opening the whole hostname for them would also open the editor. Instead, each webhook path gets its own narrow hole: a separate Access application on that path, with a bypass policy limited to the sender’s published IP ranges. In Access, the application with the more specific path wins over the one on the whole hostname. Our email policy stays second on the path, so we can still open it ourselves.
# Telegram's webhook source ranges, from core.telegram.org/bots/webhooks
resource "cloudflare_zero_trust_access_policy" "telegram_webhook" {
account_id = var.account_id
name = "n8n: Telegram webhook without login"
decision = "bypass"
include = [
{ ip = { ip = "149.154.160.0/20" } },
{ ip = { ip = "91.108.4.0/22" } },
]
}
resource "cloudflare_zero_trust_access_application" "telegram_webhook" {
account_id = var.account_id
name = "n8n: Telegram webhook"
type = "self_hosted"
domain = "n8n.example.com/webhook"
destinations = [{ type = "public", uri = "n8n.example.com/webhook" }]
policies = [
{ id = cloudflare_zero_trust_access_policy.telegram_webhook.id, precedence = 1 },
{ id = cloudflare_zero_trust_access_policy.team.id, precedence = 2 },
]
}
We follow three rules for these holes:
- A bypass always has both paths and source IPs. Our Terraform module refuses to plan a bypass with an empty IP list, because that would open the path to the whole internet.
- The workflow still checks a secret. Telegram sends the
X-Telegram-Bot-Api-Secret-Tokenheader you set when registering the webhook, and n8n checks it. IP ranges are a filter, not authentication. - If a sender does not publish its IP ranges, the path has to stay open to everyone, and the workflow must verify a signature or a shared secret on every request.
Changes go through stage
Our rule is that nobody edits a workflow on production. The editor is not the risk. The risk is what a change made there skips. The JSON in Git goes stale, and the next release from stage silently overwrites the edit. A test execution uses production credentials, so it sends real messages and writes real rows. And nobody else sees what changed or why.
So a change goes the same way every time. We build or change the workflow on stage, where imported workflows are inactive by default and run by hand. When it works, we export it to JSON and commit it to the stage branch. Then production takes exactly that commit, always with a fast-forward, so it never runs anything stage has not run first.
# stage working copy, branch "stage": export the tested workflow into the repository
docker compose -f compose.stage.yml exec -T n8n \
n8n export:workflow --id=<id> --output=/tmp/wf.json </dev/null
docker compose -f compose.stage.yml cp n8n:/tmp/wf.json workflows/orders-sync.json
git add workflows/orders-sync.json && git commit -m "orders-sync: retry on 429"
git push origin stage
# production working copy, branch "main": take exactly that commit, then import and publish
cd /srv/n8n
git fetch origin && git merge --ff-only origin/stage # refuses if main has anything stage lacks
docker compose -f compose.prod.yml cp workflows/orders-sync.json n8n:/tmp/wf.json
docker compose -f compose.prod.yml exec -T n8n n8n import:workflow --input=/tmp/wf.json </dev/null
docker compose -f compose.prod.yml exec -T n8n n8n publish:workflow --id=<id> </dev/null
The exported JSON keeps the workflow ID, so the import replaces the workflow instead of creating a copy. -T and </dev/null make docker compose exec behave in a script the same way it does in a terminal.
Two things to decide up front:
- Credentials. Our stage and production share
N8N_ENCRYPTION_KEY, so credentials can move between them. The trade-off is that stage becomes as sensitive as production, so it sits behind the same Access policy. Where an API offers read-only keys, stage gets those. - Shared rate limits. When stage and production call the same API with the same account, they share its limits. In our marketplace analytics project, stage workflows never run on a schedule, only by hand and outside the minutes when production collects data.
One change is fine to make directly on production: switching a broken workflow off. The fix then goes through stage like everything else.
Pinned versions and an upgrade routine
Every image has an exact tag: n8n:2.48.7, runners:2.48.7, postgres:18.6-alpine, cloudflared:2026.10.3. Never latest, and cloudflared runs with --no-autoupdate. An upgrade is a commit, and it goes through stage like a workflow change:
- Read the release notes between the two versions, looking for breaking changes and renamed settings. On our last upgrade, from 2.46.6 to 2.48.7, the same commit replaced
WEBHOOK_URLwithN8N_WEBHOOK_URL. - Bump n8n and the runners together on stage. They must run the same version.
- Compare the runner config with the new image. By default, the runner allows only
cryptoandmomentin JavaScript and no modules in Python. Our mountedtask-runners.jsonwidens that allowlist and must match the image in everything else. - Run the smoke test. It executes a small workflow with one JavaScript and one Python Code node and fails unless both produce output.
- Run the real workflows on stage, including webhooks.
- Release to production: take a fresh backup, fast-forward
main, and rundocker compose -f compose.prod.yml up -d.
# step 3: the launcher config shipped in the new runner image vs ours
diff <(docker run --rm --entrypoint cat n8nio/runners:2.48.7 /etc/n8n-task-runners.json) \
n8n/task-runners.json
# step 4
tools/smoke-test.sh stage
Only one environment changes at a time. If the new version breaks something, production is still on the old one.
Backups
Every night, production gets a pg_dump of the n8n database and a copy of .env with the encryption key, kept for 14 days. An encrypted copy goes offsite, and we test that it restores. The full setup, scripts included, is in Offsite backups for self-hosted n8n. Stage is not in the nightly backup, and its workflows are in Git anyway.
Checklist
- Stage and production each have their own compose file, project name, database, volumes and tunnel
- There is no default compose file, and every command has
-f - PostgreSQL, with a healthcheck that n8n waits for
- n8n is bound to
127.0.0.1, and public traffic comes only through Cloudflare Tunnel - Access covers the whole hostname, and webhook paths are open only to the sender’s IP ranges, with a secret checked in the workflow
- Workflows live in Git, are built on stage, and reach production by fast-forward
- Every image has an exact tag, and upgrades go to stage first, with a smoke test
- Nightly backups, an offsite copy and a restore test
None of this is complicated, and most of it is a few lines of configuration. What makes it work is that production only ever runs what stage has already run.
