n8n은 n8n 사이트에 가입해서 서비스를 사용할 수도 있고, 직접 셀프 호스팅으로 설치해서 사용할 수도 있습니다. 가격은 Starter가 월24유로(약38,000원), Pro가 월60유로(약96,000원)으로 꽤 비용이 나가는 편입니다. https://n8n.io/pricing/
services:
n8n:
image: docker.n8n.io/n8nio/n8n # n8n Docker 이미지 사용
restart: always # 컨테이너 종료 시 항상 재시작
ports:
- "5678:5678" # 호스트의 5678 포트를 컨테이너의 5678 포트에 연결 (n8n 기본 포트)
environment:
# n8n 라이선스 키 (선택 사항, 없으면 기본 기능으로 실행)
# - N8N_LICENSE_KEY=YOUR_LICENSE_KEY
# 데이터베이스 설정 (PostgreSQL 사용 권장)
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=n8n_db
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n_database
- DB_POSTGRESDB_USER=n8n_user
- DB_POSTGRESDB_PASSWORD=your_n8n_db_password # 안전한 비밀번호로 변경!
# 웹훅 URL 설정 (선택 사항이지만 외부 접근 시 중요)
- N8N_HOST=your.domain.com # n8n에 접속할 도메인 주소
- N8N_PROTOCOL=https # HTTPS 사용 시
- WEBHOOK_URL=https://your.domain.com/ # 웹훅 URL
volumes:
- ~/.n8n:/home/node/.n8n # n8n 데이터를 호스트에 영구 저장 (볼륨 마운트)
n8n_db: # PostgreSQL 데이터베이스 서비스
image: postgres:13 # PostgreSQL 13 버전 이미지 사용
restart: always
environment:
- POSTGRES_DB=n8n_database
- POSTGRES_USER=n8n_user
- POSTGRES_PASSWORD=your_n8n_db_password # n8n 서비스의 DB_POSTGRESDB_PASSWORD와 동일하게 설정
volumes:
- pg_data:/var/lib/postgresql/data # PostgreSQL 데이터를 호스트에 영구 저장 (볼륨 마운트)
volumes:
pg_data: # PostgreSQL 데이터 볼륨 정의
입력이 완료되면 vi editor에서 esc를 누른 후 다음 명령을 입력하여 저장합니다.
:wq
n8n 컨테이너 실행
docker-compose.yml 파일이 있는 디렉토리에서 다음 명령어를 실행합니다
docker-compose up -d
컨테이너가 잘 실행되었는지는 아래 명령어를 사용합니다.
docker-compose ps
컨테이너 로그도 확인해 줍니다.
docker-compose logs
도메인 설정하기
n8n에서 사용할 도메인은 agent.codegear.info입니다. 이를 위해 AWS Route53에 A 레코드 생성해야 합니다. 생성방법은 다음과 같습니다.
codegear.info 호스팅 영역 클릭:
"호스팅 영역" 목록에서 codegear.info를 클릭하여 상세 페이지로 들어갑니다.
레코드 생성:
"레코드 생성(Create record)" 버튼을 클릭합니다.
레코드 설정:
레코드 이름(Record name):
agent를 입력합니다. 이렇게 하면 agent.codegear.info가 됩니다. (루트 도메인 codegear.info에 연결하려면 비워둡니다.)
레코드 유형(Record type):
A - IPV4 주소로 트래픽 라우팅을 선택합니다. (도메인을 IP 주소에 연결하는 가장 일반적인 유형입니다.)
값(Value):
Nginx 및 N8N이 설치된 AWS EC2 인스턴스의 퍼블릭 IP 주소를 입력합니다. (가급적 탄력적 IP 주소를 사용하는 것을 권장합니다. 탄력적 IP는 인스턴스 재부팅 시 IP가 변경되지 않습니다.)
예: 52.78.XXX.XXX
TTL(Time to Live):
기본값 (300초)을 유지하거나 원하는 값으로 설정합니다. TTL이 짧을수록 DNS 변경 사항이 더 빨리 전파되지만, DNS 서버에 더 많은 부하를 줍니다.
라우팅 정책(Routing policy):
단순 라우팅(Simple routing)을 유지합니다.
레코드 생성:
"레코드 생성" 버튼을 클릭하여 설정을 저장합니다.
DNS 전파 확인
터미널을 열어 다음 명령으로 DNS 전파를 확인할 수 있습니다.
nslookup agent.codegear.info
Nginx 설정
Nginx 설정 파일 구조 이해
Amazon Linux 2023의 Nginx는 일반적으로 /etc/nginx/nginx.conf가 메인 설정 파일이고, 이 파일 안에서 include /etc/nginx/conf.d/*.conf; 지시어를 통해 /etc/nginx/conf.d/ 디렉토리 안의 .conf 파일들을 불러옵니다. 따라서 우리는 주로 /etc/nginx/conf.d/agent.codegear.info.conf와 같은 별도의 파일을 생성하여 도메인별 설정을 관리하는 것이 좋습니다.
새로운 Nginx 설정 파일 생성: SSH로 EC2 인스턴스에 접속한 후, 다음 명령어를 사용하여 /etc/nginx/conf.d/ 디렉토리에 agent.codegear.info.conf 파일을 생성하고 편집합니다.
파일 내용 붙여넣기: vi 에디터에 다음 내용을 붙여넣으세요. 이 설정은 agent.codegear.info에 대한 HTTP/HTTPS 처리 및 N8N으로의 리버스 프록시를 담당합니다.내용을 붙여넣은 후 ecs를 누르고 :wq! 엔터를 입력하여 저장하고 종료합니다.
server {
listen 80;
listen [::]:80;
server_name agent.codegear.info;
# Certbot will use this location to verify domain ownership
location /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html; # Ensure this directory exists and is readable by Nginx
}
}
Certbot 실행 (필수): 위 Nginx 설정 파일은 agent.codegear.info에 대한 SSL 인증서가 /etc/letsencrypt/live/agent.codegear.info/ 경로에 있다고 가정합니다. 이 인증서는 Certbot을 실행해야 발급됩니다.
Certbot이 agent.codegear.info 도메인을 인식하고, 해당 도메인에 대한 SSL 인증서를 발급받은 후, agent.codegear.info.conf 파일 내의 ssl_certificate와 ssl_certificate_key 경로를 자동으로 채우거나 확인해 줍니다.
HTTP 트래픽을 HTTPS로 리디렉션할지 물으면 2: Redirect를 선택하는 것이 좋습니다.
sudo certbot --nginx -d agent.codegear.info
Nginx 설정 테스트 및 재시작: 이제 새로운 설정 파일이 올바른지 확인하고 Nginx를 다시 시작하여 변경 사항을 적용합니다.sudo nginx -t 명령어가 syntax is ok와 test is successful을 반환해야 합니다.
sudo nginx -t # Nginx 설정 파일 문법 검사
sudo systemctl restart nginx # Nginx 서비스 재시작
Certbot이 설정 파일에 SSL 관련된 셋팅을 자동으로 추가했으므로 이부분에 n8n이 사용하는 5678 포트로 redirect 처리가 필요합니다. 아래 내용을 ssl 아래에 추가하면 됩니다.
# --- Proxy headers for N8N ---
# Ensures N8N gets correct client IP, protocol, and host information.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
# --- WebSocket support for N8N UI ---
# Crucial for N8N's real-time updates and interactive UI.
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# --- Reverse proxy to N8N ---
location / {
proxy_pass http://localhost:5678/; # <-- Updated N8N internal port
proxy_http_version 1.1;
proxy_buffering off;
proxy_read_timeout 300s; # Increased timeout for long-running workflows
proxy_send_timeout 300s;
}
전체 설정 파일은 다음과 같습니다.
# --- HTTP Block: Handles HTTP requests and redirects to HTTPS ---
server {
listen 80;
listen [::]:80; # IPv6 listening
server_name agent.codegear.info;
# Certbot's domain validation challenge location.
# This must be present in the HTTP block to allow Certbot to renew certificates.
location ~ /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html; # Ensure this path is correct and accessible
}
# All other HTTP requests are permanently redirected to HTTPS.
# Certbot usually inserts/modifies this.
return 301 https://$host$request_uri;
}
# --- HTTPS Block: Handles secure (HTTPS) requests and proxies to N8N ---
server {
listen 443 ssl;
listen [::]:443 ssl; # IPv6 listening
http2 on; # Correct way to enable HTTP/2
server_name agent.codegear.info;
# --- Let's Encrypt (Certbot) managed SSL certificate paths ---
# These paths are filled in by Certbot after successful certificate issuance.
ssl_certificate /etc/letsencrypt/live/agent.codegear.info/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/agent.codegear.info/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf; # Certbot's recommended SSL options
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # Diffie-Hellman parameters for stronger security
# --- Proxy headers for N8N ---
# Ensures N8N gets correct client IP, protocol, and host information.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
# --- WebSocket support for N8N UI ---
# Crucial for N8N's real-time updates and interactive UI.
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# --- Reverse proxy to N8N ---
location / {
proxy_pass http://localhost:5678/; # <-- Updated N8N internal port
proxy_http_version 1.1;
proxy_buffering off;
proxy_read_timeout 300s; # Increased timeout for long-running workflows
proxy_send_timeout 300s;
}
# Optional: Error pages
# error_page 404 /404.html;
# location = /404.html { }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html { }
}