【小记】服务器部署 n8n

type
Post
status
Published
summary
n8n 的 Linux 部署教程,记录了一些实践过程中遇到的坑
slug
vpn-docker-aliyun
date
Jun 4, 2025
tags
Docker
VPN
category
实践技巧
password
icon
URL
Property
Jun 4, 2025 01:44 AM

n8n介绍

n8n 是一个灵活的 AI 工作流自动化框架;在 GitHub 拥有 103k start

DockerHub 访问问题处理

原本阿里云有 Docker 的加速镜像配置,但是配置完了也没什么作用,所以只有直接配置代理进行镜像拉取了。在 Linux 中使用代理详见上一篇文章《【小记】在 Linux 中使用机场网络代理》
docker run 命令是由 Docker 守护进程(dockerd 执行的。Docker 守护进程是一个独立的后台服务,它不会自动读取你终端中设置的 http_proxyhttps_proxy 或 all_proxy 环境变量。 因此,当 dockerd 尝试去拉取 n8nio/n8n 镜像时,它会尝试直连 Docker Hub,如果直连被墙,就会出现你看到的 net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers) 错误。
要解决这个问题,需要配置 Docker 守护进程,让它也通过 Clash 代理访问 Docker Hub。
  1. 创建 Docker systemd 配置目录(如果尚未存在):
    1. sudo mkdir -p /etc/systemd/system/docker.service.d
  1. 创建或编辑代理配置文件:
    1. sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
  1. 添加如下内容(以 HTTP 和 HTTPS 代理为例,根据你的代理实际地址填写):
    1. [Service] Environment="HTTP_PROXY=http://127.0.0.1:7890" Environment="HTTPS_PROXY=http://127.0.0.1:7890" Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
      说明:
      • 127.0.0.1:7890 是常见的 Clash / V2Ray 本地代理端口。
      • NO_PROXY 是绕过本地和内网地址。
  1. 重新加载 systemd 配置并重启 Docker:
    1. sudo systemctl daemon-reexec sudo systemctl daemon-reload sudo systemctl restart docker
  1. 验证代理是否生效
    1. systemctl show --property=Environment docker # 输出:Environment=HTTP_PROXY=http://127.0.0.1:7890 ...
  1. 执行 n8n 官方提供的 Docker 命令
    1. docker run -it --rm --name n8n -d -p 5678:5678 -v n8n_data:/home/node/.n8n n8nio/n8n

服务器放行 n8n 运行端口

在云服务器控制台中配置,以阿里云为例
notion imagenotion image
notion imagenotion image
 

本地访问 n8n

直接访问报错:
Your n8n server is configured to use a secure cookie, however you are either visiting this via an insecure URL, or using Safari. To fix this, please consider the following options: Setup TLS/HTTPS (recommended), or If you are running this locally, and not using Safari, try using localhost instead If you prefer to disable this security feature (not recommended), set the environment variable N8N_SECURE_COOKIE to false
这是 n8n 默认开启了对 安全 Cookie 的支持(N8N_SECURE_COOKIE=true),这意味着访问它的 Web UI 时必须使用 HTTPS,否则它会拒绝工作或出现提示(尤其在 Safari 浏览器下更敏感)。
 
停止之前的 n8n Docker 容器:docker stop n8n,停止之后会自动删除(启动时的 —rm 参数);

解决方法一:关闭 N8N_SECURE_COOKIE(测试环境)

使用新命令启动:
docker run -it --rm --name n8n -d -p 5678:5678 -e N8N_SECURE_COOKIE=false -v n8n_data:/home/node/.n8n n8nio/n8n # -e:设置环境变量 N8N_SECURE_COOKIE=false

解决方法二:给 n8n 配置 TLS/HTTPS(生产环境)

最常见的方式是通过 反向代理 + Let's Encrypt 免费证书 实现 HTTPS,使用工具如:
  • Nginx + Certbot
  • Traefik
  • Caddy
下面是使用 Nginx + Certbot(推荐方式) 给 n8n 配置 HTTPS 的完整流程:

🧱 前提条件

  • 有一个域名,例如:n8n.yourdomain.com
  • 该域名的 A 记录已经解析到了你的服务器公网 IP
  • 已经安装了 Docker 和 n8n
  • 系统端口 80443 是开放状态(安全组 + 防火墙都已放行)

  1. 安装 Nginx 和 Certbot
    1. sudo yum install epel-release -y # CentOS sudo yum install nginx -y sudo systemctl start nginx sudo systemctl enable nginx # 安装 certbot 和 nginx 插件 sudo yum install certbot python3-certbot-nginx -y
  1. 申请 HTTPS 证书
    1. sudo certbot --nginx
      过程中输入:
      • 绑定的域名(如 n8n.yourdomain.com
      • 邮箱地址
      • 是否自动重定向 HTTP -> HTTPS(建议选“是”)
  1. 配置 Nginx 反向代理到 n8n 容器
      • 编辑配置文件:
        • sudo nano /etc/nginx/conf.d/n8n.conf
      • 添加如下内容:
        • server { listen 80; server_name n8n.yourdomain.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name n8n.yourdomain.com; ssl_certificate /etc/letsencrypt/live/n8n.yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/n8n.yourdomain.com/privkey.pem; location / { proxy_pass http://localhost:5678; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
      • 然后重启 Nginx:
        • sudo nginx -t sudo systemctl reload nginx
  1. 运行 n8n 容器(建议添加环境变量)
    1. docker run -it --rm --name n8n \ -p 5678:5678 \ -e N8N_HOST="n8n.yourdomain.com" \ -e N8N_PORT=5678 \ -e N8N_PROTOCOL=https \ -e VUE_APP_URL_BASE_API="https://n8n.yourdomain.com/" \ -e N8N_SECURE_COOKIE=true \ -v n8n_data:/home/node/.n8n \ n8nio/n8n
  1. 测试访问
    1. 在浏览器访问:https://n8n.yourdomain.com
 
访问成功
notion imagenotion image
If you have any questions, please contact me.