This project is a scalable setup for n8n using the Queue Mode architecture.
┌─────────────────────────────────────────────────────────────┐
│ External │
│ Users ──────┐ │
│ ├──► Reverse Proxy (Nginx/Traefik) │
│ Webhooks ───┘ │ │
└────────────────────────────┼────────────────────────────────┘
│
┌────────────────────────────┼────────────────────────────────┐
│ n8n Stack│ │
│ ▼ │
│ ┌─────────────────────────┐ │
│ │ n8n Main │ │
│ │ (Editor + API) │ │
│ │ :5678 │ │
│ └───────────┬─────────────┘ │
│ │ │
│ ┌────────────────┼────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Worker 1 │ │ Worker N │ │Webhook Worker│ │
│ │ │ │ │ │ :5679 │ │
│ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │
│ └───────────────┼─────────────────┘ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ Redis │ │
│ │ (Message Queue) │ │
│ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ PgBouncer │ │
│ │ (Connection Pooler) │ │
│ └──────────┬──────────┘ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ PostgreSQL 17 │ │
│ │ (Database) │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
scalable-n8n-production-ready/
├── compose.yaml # Main Docker Compose file
├── setup.sh # ✨ Automatic setup script
├── .env.example # Sample configuration (all in one file)
├── .gitignore # Files ignored by git
├── pgbouncer.ini.example # PgBouncer sample configuration
├── userlist.txt.example # PgBouncer users sample
├── init-data.sh # DB user creation script
└── README.md # This file
| Service | Image | Purpose | Port |
|---|---|---|---|
| PostgreSQL 17 | postgres:17 |
Main database | 5432 (internal) |
| PgBouncer | edoburu/pgbouncer:v1.24.1-p0 |
Connection Pooling | 6432 (internal) |
| Redis | redis:7-alpine |
Message Queue | 6379 (internal) |
| n8n Main | ghcr.io/chosomeister/n8n:enterprise |
Editor/API | 5678 |
| Worker | ghcr.io/chosomeister/n8n:enterprise |
Workflow execution | - |
| Webhook Worker | ghcr.io/chosomeister/n8n:enterprise |
Receive webhooks | 5679 |
| Variable | Description |
|---|---|
NODE_ENV=production |
Production mode for Node.js |
N8N_SECURE_COOKIE=true |
Send cookies only over HTTPS |
N8N_ENCRYPTION_KEY |
Credential encryption (must be unique) |
N8N_BLOCK_ENV_ACCESS_IN_NODE=true |
Prevent env access in Code nodes |
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true |
Check file permissions |
QUEUE_BULL_REDIS_PASSWORD |
Redis password |
# Encryption Key
openssl rand -hex 32
# Redis Password
openssl rand -base64 24
# Database Password
openssl rand -base64 24This setup requires two separate domains:
| Domain | Service | Internal Port |
|---|---|---|
https://n8n.yourdomain.com |
n8n Main (UI/API) | 5678 |
https://n8n-webhook.yourdomain.com |
Webhook Worker | 5679 |
In .env-main:
N8N_HOST=n8n.yourdomain.com
N8N_PROTOCOL=https
N8N_EDITOR_BASE_URL=https://n8n.yourdomain.com
WEBHOOK_URL=https://n8n-webhook.yourdomain.com| Service | Memory Limit | Memory Reserved |
|---|---|---|
| n8n Main | 2GB | 1GB |
| Worker | 1GB | 512MB |
| Webhook Worker | 1GB | 512MB |
pgbouncer.ini file:
| Setting | Value | Description |
|---|---|---|
pool_mode |
transaction | Each query gets a new connection |
max_client_conn |
1000 | Maximum connections from clients |
default_pool_size |
50 | Concurrent connections to PostgreSQL |
min_pool_size |
5 | Minimum open connections |
reserve_pool_size |
20 | Emergency connections |
userlist.txt file:
"n8n_user" "YOUR_DB_PASSWORD"
⚠️ For better security, use md5 hash:"n8n_user" "md5<hash>"Generate md5:
echo -n "YOUR_PASSWORD+n8n_user" | md5sum
git clone https://github.com/ChosoMeister/scalable-n8n-production-ready.git
cd scalable-n8n-production-ready
./setup.sh
docker compose up -dThe setup script automatically generates secure passwords and prepares all files.
# 1. Clone
git clone https://github.com/ChosoMeister/scalable-n8n-production-ready.git
cd scalable-n8n-production-ready
# 2. Copy sample files
cp .env.example .env
cp pgbouncer.ini.example pgbouncer.ini
cp userlist.txt.example userlist.txt
# 3. Generate secure passwords and replace in files
nano .env # All settings are here
nano pgbouncer.ini # DB Password
nano userlist.txt # DB Password
# 4. Start
docker compose up -d- n8n Editor: http://localhost:5678
- Webhook Worker: http://localhost:5679
# 3 workers
docker compose up -d --scale worker=3
# 5 workers
docker compose up -d --scale worker=5Capacity Calculation:
Workers × Concurrency = Total Parallel Executions
5 workers × 10 concurrency = 50 parallel workflows
1. Trigger/Webhook ─────► n8n Main
│
2. Create Job
│
3. ─────► Redis Queue
│
4. Worker picks job
│
5. Worker ◄───────────────────┘
│
6. └───► Get workflow from PostgreSQL
│
7. └───► Execute workflow
│
8. └───► Save results to PostgreSQL
│
9. └───► Notify Redis (complete)
In .env-main:
| Variable | Value | Description |
|---|---|---|
EXECUTIONS_TIMEOUT |
3600 | Maximum execution time (seconds) |
EXECUTIONS_DATA_PRUNE |
true | Auto-delete old executions |
EXECUTIONS_DATA_MAX_AGE |
168 | Keep for 168 hours (7 days) |
EXECUTIONS_DATA_SAVE_ON_ERROR |
all | Save all errors |
EXECUTIONS_DATA_SAVE_ON_SUCCESS |
all | Save all successes |
GENERIC_TIMEZONE=Asia/Tehran
TZ=Asia/TehranImportant for correct execution of Cron triggers and Schedule nodes
# View status
docker compose ps
# Logs
docker compose logs -f n8n
docker compose logs -f worker
docker compose logs -f webhook-worker
# Stop
docker compose down
# Stop + delete volumes
docker compose down -v
# Restart a service
docker compose restart n8n
# Check config
docker compose configThis setup does not include a reverse proxy. You need to configure separately:
- Nginx
- Traefik
- Caddy
- HAProxy
Make sure to use HTTPS:
N8N_SECURE_COOKIE=truerequires HTTPS- Webhooks are not secure without HTTPS
Backup the volumes:
db_storage- Databasen8n_storage- n8n files
# Backup database
docker compose exec postgres pg_dump -U lucas n8n > backup.sqlIf behind a reverse proxy:
N8N_TRUSTED_PROXIES=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
⚠️ Never use*!
- Unique
N8N_ENCRYPTION_KEYis set - Strong
REDIS_PASSWORDis set - Database passwords are strong
- Real domains are configured
- HTTPS is enabled
- Reverse proxy is configured
- You have a backup strategy
- Monitoring is set up
این پروژه یک setup مقیاسپذیر برای n8n است که از معماری Queue Mode استفاده میکند.
┌─────────────────────────────────────────────────────────────┐
│ External │
│ Users ──────┐ │
│ ├──► Reverse Proxy (Nginx/Traefik) │
│ Webhooks ───┘ │ │
└────────────────────────────┼────────────────────────────────┘
│
┌────────────────────────────┼────────────────────────────────┐
│ n8n Stack│ │
│ ▼ │
│ ┌─────────────────────────┐ │
│ │ n8n Main │ │
│ │ (Editor + API) │ │
│ │ :5678 │ │
│ └───────────┬─────────────┘ │
│ │ │
│ ┌────────────────┼────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Worker 1 │ │ Worker N │ │Webhook Worker│ │
│ │ │ │ │ │ :5679 │ │
│ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │
│ └───────────────┼─────────────────┘ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ Redis │ │
│ │ (Message Queue) │ │
│ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ PgBouncer │ │
│ │ (Connection Pooler) │ │
│ └──────────┬──────────┘ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ PostgreSQL 17 │ │
│ │ (Database) │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
scalable-n8n-production-ready/
├── compose.yaml # Docker Compose اصلی
├── setup.sh # ✨ اسکریپت نصب خودکار
├── .env.example # نمونه تنظیمات (همه در یک فایل)
├── .gitignore # فایلهای ignore شده در git
├── pgbouncer.ini.example # نمونه تنظیمات PgBouncer
├── userlist.txt.example # نمونه یوزرهای PgBouncer
├── init-data.sh # اسکریپت ایجاد یوزر DB
└── README.md # این فایل
| سرویس | Image | وظیفه | پورت |
|---|---|---|---|
| PostgreSQL 17 | postgres:17 |
دیتابیس اصلی | 5432 (internal) |
| PgBouncer | edoburu/pgbouncer:v1.24.1-p0 |
Connection Pooling | 6432 (internal) |
| Redis | redis:7-alpine |
Message Queue | 6379 (internal) |
| n8n Main | ghcr.io/chosomeister/n8n:enterprise |
Editor/API | 5678 |
| Worker | ghcr.io/chosomeister/n8n:enterprise |
اجرای workflows | - |
| Webhook Worker | ghcr.io/chosomeister/n8n:enterprise |
دریافت webhooks | 5679 |
| Variable | توضیح |
|---|---|
NODE_ENV=production |
حالت production برای Node.js |
N8N_SECURE_COOKIE=true |
ارسال cookies فقط روی HTTPS |
N8N_ENCRYPTION_KEY |
رمزنگاری credentials (باید منحصر به فرد باشد) |
N8N_BLOCK_ENV_ACCESS_IN_NODE=true |
جلوگیری از دسترسی به env در Code nodes |
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true |
بررسی دسترسی فایلها |
QUEUE_BULL_REDIS_PASSWORD |
پسورد Redis |
# Encryption Key
openssl rand -hex 32
# Redis Password
openssl rand -base64 24
# Database Password
openssl rand -base64 24این setup نیاز به دو دامنه جداگانه دارد:
| دامنه | سرویس | پورت داخلی |
|---|---|---|
https://n8n.yourdomain.com |
n8n Main (UI/API) | 5678 |
https://n8n-webhook.yourdomain.com |
Webhook Worker | 5679 |
در .env-main:
N8N_HOST=n8n.yourdomain.com
N8N_PROTOCOL=https
N8N_EDITOR_BASE_URL=https://n8n.yourdomain.com
WEBHOOK_URL=https://n8n-webhook.yourdomain.com| سرویس | Memory Limit | Memory Reserved |
|---|---|---|
| n8n Main | 2GB | 1GB |
| Worker | 1GB | 512MB |
| Webhook Worker | 1GB | 512MB |
فایل pgbouncer.ini:
| Setting | مقدار | توضیح |
|---|---|---|
pool_mode |
transaction | هر query اتصال جدید میگیرد |
max_client_conn |
1000 | حداکثر اتصال از clients |
default_pool_size |
50 | اتصالات همزمان به PostgreSQL |
min_pool_size |
5 | حداقل اتصالات باز |
reserve_pool_size |
20 | اتصالات اضطراری |
فایل userlist.txt:
"n8n_user" "YOUR_DB_PASSWORD"
⚠️ برای امنیت بیشتر از md5 hash استفاده کنید:"n8n_user" "md5<hash>"تولید md5:
echo -n "YOUR_PASSWORD+n8n_user" | md5sum
git clone https://github.com/ChosoMeister/scalable-n8n-production-ready.git
cd scalable-n8n-production-ready
./setup.sh
docker compose up -dاسکریپت setup به صورت خودکار پسوردهای امن تولید میکنه و همه فایلها رو آماده میکنه.
# 1. Clone
git clone https://github.com/ChosoMeister/scalable-n8n-production-ready.git
cd scalable-n8n-production-ready
# 2. کپی فایلهای نمونه
cp .env.example .env
cp pgbouncer.ini.example pgbouncer.ini
cp userlist.txt.example userlist.txt
# 3. تولید پسوردهای امن و جایگزینی در فایلها
nano .env # تمام تنظیمات اینجاست
nano pgbouncer.ini # DB Password
nano userlist.txt # DB Password
# 4. شروع
docker compose up -d- n8n Editor: http://localhost:5678
- Webhook Worker: http://localhost:5679
# 3 worker
docker compose up -d --scale worker=3
# 5 worker
docker compose up -d --scale worker=5محاسبه ظرفیت:
Workers × Concurrency = Total Parallel Executions
5 workers × 10 concurrency = 50 workflow همزمان
1. Trigger/Webhook ─────► n8n Main
│
2. Create Job
│
3. ─────► Redis Queue
│
4. Worker picks job
│
5. Worker ◄───────────────────┘
│
6. └───► Get workflow from PostgreSQL
│
7. └───► Execute workflow
│
8. └───► Save results to PostgreSQL
│
9. └───► Notify Redis (complete)
در .env-main:
| Variable | مقدار | توضیح |
|---|---|---|
EXECUTIONS_TIMEOUT |
3600 | حداکثر زمان اجرا (ثانیه) |
EXECUTIONS_DATA_PRUNE |
true | حذف خودکار execution های قدیمی |
EXECUTIONS_DATA_MAX_AGE |
168 | نگهداری تا 168 ساعت (7 روز) |
EXECUTIONS_DATA_SAVE_ON_ERROR |
all | ذخیره همه خطاها |
EXECUTIONS_DATA_SAVE_ON_SUCCESS |
all | ذخیره همه موفقیتها |
GENERIC_TIMEZONE=Asia/Tehran
TZ=Asia/Tehranمهم برای اجرای صحیح Cron triggers و Schedule nodes
# مشاهده وضعیت
docker compose ps
# لاگها
docker compose logs -f n8n
docker compose logs -f worker
docker compose logs -f webhook-worker
# Stop
docker compose down
# Stop + حذف volumes
docker compose down -v
# Restart یک سرویس
docker compose restart n8n
# بررسی config
docker compose configاین setup شامل reverse proxy نیست. باید جداگانه تنظیم کنید:
- Nginx
- Traefik
- Caddy
- HAProxy
حتماً از HTTPS استفاده کنید:
N8N_SECURE_COOKIE=trueنیاز به HTTPS دارد- Webhooks امن نیستند بدون HTTPS
Volume ها را backup کنید:
db_storage- دیتابیسn8n_storage- فایلهای n8n
# Backup database
docker compose exec postgres pg_dump -U lucas n8n > backup.sqlاگر پشت reverse proxy هستید:
N8N_TRUSTED_PROXIES=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
⚠️ هرگز از*استفاده نکنید!
-
N8N_ENCRYPTION_KEYمنحصر به فرد تنظیم شده -
REDIS_PASSWORDقوی تنظیم شده - پسوردهای دیتابیس قوی هستند
- دامنههای واقعی تنظیم شده
- HTTPS فعال است
- Reverse proxy تنظیم شده
- Backup strategy دارید
- Monitoring تنظیم شده