⚙️ Environment Variables Configuration
Environment variables là cách configure n8n cho production. Bài này covers tất cả settings quan trọng bạn cần biết.
Environment Variables Basics
How to Set:
Bash
1# Method 1: Docker run2docker run -e VAR_NAME=value n8nio/n8n3 4# Method 2: docker-compose.yml5services:6 n8n:7 environment:8 - VAR_NAME=value9 10# Method 3: .env file11VAR_NAME=value12 13# Method 4: Export (dev only)14export VAR_NAME=valuePriority (highest to lowest):
Text
11. Command line -e flags22. docker-compose.yml environment33. .env file44. Default valuesCore Settings
Basic Configuration:
Bash
1# Instance Settings2N8N_HOST=n8n.yourdomain.com3N8N_PORT=56784N8N_PROTOCOL=https5 6# Webhook URL (Critical!)7WEBHOOK_URL=https://n8n.yourdomain.com/8 9# Editor Base URL10N8N_EDITOR_BASE_URL=https://n8n.yourdomain.com/11 12# Timezone13GENERIC_TIMEZONE=Asia/Ho_Chi_Minh14TZ=Asia/Ho_Chi_MinhImportant
WEBHOOK_URL phải match với public URL của n8n. Webhooks sẽ không work nếu sai!
Path Settings:
Bash
1# Data folder location2N8N_USER_FOLDER=/home/node/.n8n3 4# Config file path5N8N_CONFIG_FILES=/config/custom.json6 7# Custom nodes path8N8N_CUSTOM_EXTENSIONS=/custom-nodesDatabase Configuration
SQLite (Default):
Bash
1# Default, no config needed2# Data stored in N8N_USER_FOLDER/database.sqlitePostgreSQL:
Bash
1DB_TYPE=postgresdb2DB_POSTGRESDB_HOST=postgres3DB_POSTGRESDB_PORT=54324DB_POSTGRESDB_DATABASE=n8n5DB_POSTGRESDB_USER=n8n6DB_POSTGRESDB_PASSWORD=secure_password7DB_POSTGRESDB_SCHEMA=public8 9# Optional: SSL connection10DB_POSTGRESDB_SSL_CA=/path/to/ca.crt11DB_POSTGRESDB_SSL_CERT=/path/to/client.crt12DB_POSTGRESDB_SSL_KEY=/path/to/client.key13DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED=trueMySQL:
Bash
1DB_TYPE=mysqldb2DB_MYSQLDB_HOST=mysql3DB_MYSQLDB_PORT=33064DB_MYSQLDB_DATABASE=n8n5DB_MYSQLDB_USER=n8n6DB_MYSQLDB_PASSWORD=secure_passwordConnection Pool:
Bash
1# Tune for performance2DB_POSTGRESDB_POOL_SIZE=10Authentication
Basic Auth (Simple):
Bash
1N8N_BASIC_AUTH_ACTIVE=true2N8N_BASIC_AUTH_USER=admin3N8N_BASIC_AUTH_PASSWORD=super_secure_password_1234 5# Hash the password (optional, more secure)6N8N_BASIC_AUTH_HASH=trueJWT Auth (For API access):
Bash
1N8N_JWT_AUTH_ACTIVE=true2N8N_JWT_AUTH_HEADER=Authorization3N8N_JWKS_URI=https://your-auth-server/.well-known/jwks.jsonUser Management (Enterprise-like):
Bash
1# Enable user management2N8N_USER_MANAGEMENT_DISABLED=false3 4# Email config for invitations5N8N_EMAIL_MODE=smtp6N8N_SMTP_HOST=smtp.gmail.com7N8N_SMTP_PORT=5878N8N_SMTP_USER=your@email.com9N8N_SMTP_PASS=app_password10N8N_SMTP_SENDER=noreply@yourdomain.com11N8N_SMTP_SSL=trueExecution Settings
Execution Mode:
Bash
1# Regular mode (default)2EXECUTIONS_MODE=regular3 4# Queue mode (for scaling)5EXECUTIONS_MODE=queue6QUEUE_BULL_REDIS_HOST=redis7QUEUE_BULL_REDIS_PORT=63798QUEUE_BULL_REDIS_PASSWORD=redis_password9QUEUE_BULL_REDIS_DB=0Execution Limits:
Bash
1# Timeout (milliseconds, -1 = no limit)2EXECUTIONS_TIMEOUT=3600000 # 1 hour3EXECUTIONS_TIMEOUT_MAX=7200000 # 2 hours max4 5# Data size limits6N8N_PAYLOAD_SIZE_MAX=16 # MB7 8# Process limits9EXECUTIONS_PROCESS=own # "own" or "main"Execution Data Retention:
Bash
1# Auto-prune old executions2EXECUTIONS_DATA_PRUNE=true3EXECUTIONS_DATA_MAX_AGE=168 # Hours (168 = 7 days)4EXECUTIONS_DATA_SAVE_ON_ERROR=all5EXECUTIONS_DATA_SAVE_ON_SUCCESS=all6EXECUTIONS_DATA_SAVE_ON_PROGRESS=false7EXECUTIONS_DATA_SAVE_MANUAL_EXECUTIONS=trueConcurrency:
Bash
1# Max concurrent executions2N8N_CONCURRENCY_PRODUCTION_LIMIT=203 4# For queue mode5QUEUE_BULL_REDIS_TIMEOUT_THRESHOLD=300006QUEUE_RECOVERY_INTERVAL=60Webhook Settings
Webhook Configuration:
Bash
1# Base URL for webhooks2WEBHOOK_URL=https://n8n.yourdomain.com/3 4# Webhook path prefix5N8N_PATH=/6 7# Disable tunnel (production)8N8N_TUNNEL_DISABLED=trueWebhook Security:
Bash
1# Allowed webhook methods2N8N_WEBHOOK_METHODS=GET,POST,PUT,DELETE3 4# Custom headers5N8N_HEADER_FORWARDED=X-Forwarded-ForSecurity Settings
Encryption:
Bash
1# Encryption key for credentials (Auto-generated if not set)2N8N_ENCRYPTION_KEY=your-32-char-encryption-key-here3 4# IMPORTANT: Back this up! Can't decrypt credentials without itIP Restrictions:
Bash
1# Restrict editor access by IP2N8N_EDITOR_ACCESS_ALLOWED_IPS=192.168.1.0/24,10.0.0.0/8Disable Features:
Bash
1# Disable execution test feature2N8N_DISABLE_UI=false3 4# Disable production webhook test5N8N_DISABLE_PRODUCTION_MAIN_PROCESS=falseLogging
Log Configuration:
Bash
1# Log level2N8N_LOG_LEVEL=info # error, warn, info, verbose, debug3 4# Log output5N8N_LOG_OUTPUT=console,file6 7# Log file location8N8N_LOG_FILE_LOCATION=/logs/n8n.log9N8N_LOG_FILE_SIZE_MAX=50 # MB10N8N_LOG_FILE_COUNT_MAX=100Error Tracking:
Bash
1# Sentry integration2N8N_SENTRY_DSN=https://xxx@sentry.io/xxx3 4# Diagnostics5N8N_DIAGNOSTICS_ENABLED=true6N8N_DIAGNOSTICS_CONFIG_FRONTEND=7N8N_DIAGNOSTICS_CONFIG_BACKEND=Performance Tuning
Memory & CPU:
Bash
1# Node.js memory limit2NODE_OPTIONS=--max-old-space-size=40963 4# Worker threads5N8N_RUNNERS_N8N_TASK_BROKER_URI=localhost:5679Lazy Loading:
Bash
1# Load nodes on demand2N8N_LAZY_LOAD_NODES=true3 4# Pre-warm certain nodes5N8N_PRE_LOADED_NODES=n8n-nodes-base.Webhook,n8n-nodes-base.HttpExternal Services
S3 for Binary Data:
Bash
1# Store binary data in S3 (recommended for production)2N8N_AVAILABLE_BINARY_DATA_MODES=filesystem,s33N8N_DEFAULT_BINARY_DATA_MODE=s34 5# S3 Configuration6N8N_EXTERNAL_STORAGE_S3_HOST=s3.amazonaws.com7N8N_EXTERNAL_STORAGE_S3_BUCKET_NAME=n8n-binary-data8N8N_EXTERNAL_STORAGE_S3_BUCKET_REGION=ap-southeast-19N8N_EXTERNAL_STORAGE_S3_ACCESS_KEY=AKIA...10N8N_EXTERNAL_STORAGE_S3_ACCESS_SECRET=xxxExternal Hooks:
Bash
1# Custom hooks2EXTERNAL_HOOK_FILES=/hooks/custom.jsComplete Production .env
Bash
1# ===================================2# N8N PRODUCTION CONFIGURATION3# ===================================4 5# ----- CORE -----6N8N_HOST=n8n.yourdomain.com7N8N_PORT=56788N8N_PROTOCOL=https9WEBHOOK_URL=https://n8n.yourdomain.com/10N8N_EDITOR_BASE_URL=https://n8n.yourdomain.com/11 12# ----- TIMEZONE -----13GENERIC_TIMEZONE=Asia/Ho_Chi_Minh14TZ=Asia/Ho_Chi_Minh15 16# ----- DATABASE -----17DB_TYPE=postgresdb18DB_POSTGRESDB_HOST=postgres19DB_POSTGRESDB_PORT=543220DB_POSTGRESDB_DATABASE=n8n21DB_POSTGRESDB_USER=n8n22DB_POSTGRESDB_PASSWORD=CHANGE_THIS_PASSWORD23 24# ----- AUTHENTICATION -----25N8N_BASIC_AUTH_ACTIVE=true26N8N_BASIC_AUTH_USER=admin27N8N_BASIC_AUTH_PASSWORD=CHANGE_THIS_PASSWORD28 29# ----- SECURITY -----30N8N_ENCRYPTION_KEY=GENERATE_32_CHAR_KEY_HERE31N8N_TUNNEL_DISABLED=true32 33# ----- EXECUTION -----34EXECUTIONS_MODE=regular35EXECUTIONS_TIMEOUT=360000036EXECUTIONS_DATA_PRUNE=true37EXECUTIONS_DATA_MAX_AGE=16838N8N_CONCURRENCY_PRODUCTION_LIMIT=2039 40# ----- LOGGING -----41N8N_LOG_LEVEL=info42N8N_LOG_OUTPUT=console43 44# ----- PERFORMANCE -----45NODE_OPTIONS=--max-old-space-size=2048Environment Variables Reference
Quick Reference Table:
Text
1CATEGORY VARIABLE DEFAULT2─────────────────────────────────────────────────────────3Core N8N_HOST localhost4 N8N_PORT 56785 N8N_PROTOCOL http6 WEBHOOK_URL http://localhost:5678/7 8Database DB_TYPE sqlite9 DB_POSTGRESDB_* (various)10 11Auth N8N_BASIC_AUTH_ACTIVE false12 N8N_BASIC_AUTH_USER -13 N8N_BASIC_AUTH_PASSWORD -14 15Execution EXECUTIONS_MODE regular16 EXECUTIONS_TIMEOUT -117 EXECUTIONS_DATA_PRUNE false18 19Queue QUEUE_BULL_REDIS_HOST localhost20 QUEUE_BULL_REDIS_PORT 637921 22Logging N8N_LOG_LEVEL info23 N8N_LOG_OUTPUT consoleBài Tập Thực Hành
Configuration Exercise
Create your production .env:
- Copy template above
- Generate encryption key:
Bash1openssl rand -hex 16
- Generate passwords:
Bash1openssl rand -base64 24
- Configure database settings
- Test locally with docker-compose
- Verify webhooks work
Store .env securely (never in git)! 🔐
Key Takeaways
Remember
- 🔑 N8N_ENCRYPTION_KEY - Back it up!
- 🌐 WEBHOOK_URL - Must match public URL
- 🔒 Strong passwords - Generate, don't guess
- 📝 Document - Keep track of what you changed
- 🚫 Never commit .env to version control
Tiếp Theo
Bài tiếp theo: Database Setup - PostgreSQL setup chi tiết, migrations, và optimization.
