1、token模式运行

国内docker-compose.yaml **

1
2
3
4
5
6
7
8
9
10
11
version: '3'
services:
cloudflared:
image: cloudflare/cloudflared:latest
container_name: cloudflared
command: tunnel --no-autoupdate run --token eyJ
network_mode: host
environment:
- TUNNEL_TRANSPORT_PROTOCOL=http2
- TUNNEL_REGION=us
restart: always # 修改重启策略为“总是”

linux

直接运行

1
TUNNEL_TRANSPORT_PROTOCOL=http2 TUNNEL_REGION=us cloudflared tunnel --no-autoupdate run --token YOUR_TOKEN

pm2 运行

创建一个 JSON 文件(例如 cloudflared-pm2.json),并定义进程配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"apps": [
{
"name": "cloudflared-tunnel",
"script": "cloudflared",
"args": [
"tunnel",
"--no-autoupdate",
"run",
"--token",
"YOUR_TOKEN"
],
"cwd": "/path/to/your/cloudflared", // 替换为 cloudflared 可执行文件所在的目录
"autorestart": true,
"watch": false,
"env": {
"TUNNEL_TRANSPORT_PROTOCOL": "http2",
"TUNNEL_REGION": "us",
"NODE_ENV": "production"
}
}
]
}

使用以下命令启动 PM2 进程:

1
pm2 start cloudflared

2、json文件模式运行

2-1 创建目录cloudflared的运行文件跟tunnel.json 、tunnel.yml

1
mkdir -p cloudflared

打开https://fscarmen.cloudflare.now.cc/ 获取json配置文件,然后复制{}这段内容到tunnel.json,创建tunnel.yml,示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tunnel: YOUR_TUNNEL_ID  //json里面有显示
credentials-file: /path/to/tunnel.json //文件所在的路径,可以用pwd命令

ingress:
- hostname: example.com
service: http://localhost:3000
originRequest:
region: us # 美国
- hostname: eu.example.com
service: http://localhost:4000
originRequest:
region: eu # 欧洲
- hostname: ap.example.com
service: http://localhost:5000
originRequest:
region: ap # 亚太
- service: http_status:404

运行命令

1
/root/server/cloudflared tunnel --logfile /root/server/cloudflared.log --edge-ip-version auto --config /root/server/tunnel.yml run   //路径为文件所在路径

PM2运行

创建一个 JSON 文件(例如 cloudflared-pm2.json),并定义进程配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"apps": [
{
"name": "cloudflared-tunnel",
"script": "/root/server/cloudflared",
"args": [
"tunnel",
"--logfile",
"/root/server/cloudflared.log",
"--edge-ip-version",
"auto",
"--config",
"/root/server/tunnel.yml",
"run"
],
"cwd": "/root/server", // 工作目录
"autorestart": true, // 自动重启
"watch": false, // 不监视文件更改
"env": {
"NODE_ENV": "production"
}
}
]
}

使用以下命令启动 PM2 进程:

1
pm2 start cloudflared-pm2.json