PortGate

Blueprints · AI & LLMs

Production 25 min · n8n / Nginx

Self-hosted n8n hardened production setup

Keeps n8n on localhost, puts basic auth in front of the UI, rate-limits webhooks at Nginx, and excludes the execute-command node. Public signup stays off because this file does not enable open registration.

Live config

Baked-in protections

Step 1 · yaml

Compose file

/opt/n8n/compose.yaml

Do not publish 5678 on 0.0.0.0. Change the password before the start script.

services:
  n8n:
    image: n8nio/n8n:latest
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      N8N_HOST: 127.0.0.1
      N8N_PORT: 5678
      N8N_PROTOCOL: http
      WEBHOOK_URL: https://n8n.example.com/
      N8N_BASIC_AUTH_ACTIVE: "true"
      N8N_BASIC_AUTH_USER: admin
      N8N_BASIC_AUTH_PASSWORD: change-me
      N8N_BLOCK_ENV_ACCESS_IN_NODE: "true"
      NODES_EXCLUDE: '["n8n-nodes-base.executeCommand"]'
    security_opt:
      - no-new-privileges:true
    restart: unless-stopped

Step 2 · nginx

Nginx rate limit

/etc/nginx/sites-available/n8n.example.com.conf

Webhooks stay available and are limited. The UI is on the same host.

limit_req_zone $binary_remote_addr zone=n8nwebhook:10m rate=10r/s;
server {
    listen 443 ssl;
    server_name n8n.example.com;
    ssl_certificate /etc/letsencrypt/live/n8n.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/n8n.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    location /webhook/ {
        limit_req zone=n8nwebhook burst=20 nodelay;
        proxy_pass http://127.0.0.1:5678;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    location / {
        proxy_pass http://127.0.0.1:5678;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Step 3 · bash

Start script

/opt/n8n/up.sh

Written, not run.

#!/bin/sh
set -eu
if [ "change-me" = "change-me" ]; then
  echo "Set N8N_PASSWORD first." >&2
  exit 1
fi
cd "/opt/n8n"
docker compose up -d