跳转至

Cloudflare Tunnel配置多个域名

我的环境是树莓派,在调试通了一个域名后,再配置一个服务即可配置多个域名.

配置一个域名:

1 下载 cloudflared :

uname -a中出现armv7l字样,从github下载32位arm版本的cloudflared: https://github.com/cloudflare/cloudflared/releases

wget https://github.com/cloudflare/cloudflared/releases/download/2022.10.2/cloudflared-linux-arm
mv cloudflared-linux-arm cloudflared
chmod +x cloudflared
sudo cp cloudflared /usr/loca/bin/
2 登录cloudflared

执行cloudflared tunnel login, 输出如下:

$ cloudflared tunnel login
A browser window should have opened at the following URL:

https://dash.cloudflare.com/argotunnel?callback=https%3A%2F%2Flogin.cloudflareaccess.org%2FTyR0HsQ.........2eV5WgA8%3D

If the browser failed to open, please visit the URL above directly in your browser.

在浏览器打开上述URL地址, 选择自己的希望穿透的域名. 在本地~/.cloudflared/cert.pem生成后网页会显示成功. 此时生成的是证书

3 创建 argo tunnel

cloudflared tunnel create <NAME> 这条命令会输出一个 tunnle UUID, name 可以自己随便取一个, 建议用域名

  • 查看 tunnel 列表,可以使用 cloudflared tunnel list
  • 删除 tunnel 列表,可以使用 cloudflared tunnel delete <NAME>
4 创建 DNS 记录
cloudflared tunnel route dns <UUID or NAME> <域名>

或者去网页https://dash.cloudflare.com/上进websites配置DNS: - Type : CNAME - Name : 域名 - Content : <uuid>.cfargotunnel.com

5 配置 tunnel
$ cat tunnel-isk.yml
tunnel: 可以用域名
credentials-file: /home/XXX/.cloudflared/0b44ff1a-0fae-477c-ad57-19ba2471b801.json
origincert: /home/XXX/.cloudflared/cert-isk.pem

ingress:
  - hostname: 域名
    service: http://localhost:8005
  - service: http_status:404

执行一下:

cloudflared tunnel --config ~/tunnel-isk.yml run

如果没有报错,tunnel 就成功了. 打开浏览器访问一下域名,要能能正常访问

6 配置服务

sudo cloudflared --config CONFIG-FILE service install
重启后能启动服务

配置多个域名:

重复上述步骤, 即可配置多个域名. 需要注意的是, 步骤登录cloudflared需要先把~/.cloudflared/cert.pem重命名

1 重复步骤配置其它域名

mv ~/.cloudflared/cert.pem ~/.cloudflared/cert.pem.bak

2 重复配置
  • 登录cloudflared : cloudflared tunnel login完成后mv ~/.cloudflared/cert.pem ~/.cloudflared/cert-domain.pem
  • 创建 argo tunnel : cloudflared tunnel create <NAME>
  • 创建 DNS 记录 : cloudflared tunnel route dns <UUID or NAME> <域名>
  • 配置 tunnel : 配置~/tunnel-domain.yml, 填写刚生成的credentials-file, origincert, 以及登录cloudflared选择的域名信息

此时我们手动执行cloudflared tunnel --config ~/tunnel-domain.yml run后,打开对应域名应该和配置的http://localhost:800x内容一致

最后回复上个的证书mv ~/.cloudflared/cert.pem.bak ~/.cloudflared/cert.pem

3 配置服务 :

查看服务信息:service cloudflared status

$ service cloudflared status
● cloudflared.service - cloudflared
   Loaded: loaded (/etc/systemd/system/cloudflared.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-10-22 22:26:54 CST; 1h 3min ago
 Main PID: 1095 (cloudflared)
    Tasks: 9 (limit: 2155)
   CGroup: /system.slice/cloudflared.service
           └─1095 /home/XXX/bin/cloudflared --no-autoupdate --config /etc/cloudflared/config.yml tunnel run

.....

上面显示了服务文件地址/etc/systemd/system/cloudflared.service:

$ cat /etc/systemd/system/cloudflared.service
[Unit]
Description=cloudflared
After=network.target

[Service]
TimeoutStartSec=0
Type=notify
ExecStart=/home/XXX/bin/cloudflared --no-autoupdate --config /etc/cloudflared/config.yml tunnel run
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

可以看到ExecStart既是我们手动执行的命令. 复制此文件, 配置其它域名. 我们只修改--config后的配置信息,类似如下效果

$ cat /etc/systemd/system/vtube.service 
[Unit]
Description=cloudflared
After=network.target

[Service]
TimeoutStartSec=0
Type=notify
ExecStart=/home/XXX/bin/cloudflared --no-autoupdate --config /www/tunnel-vtube.yml tunnel run
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

启动服务vtube

sudo service vtube start
配置服务开机启动:
sudo systemctl enable vtube

查看服务状态:service vtube status

$ service vtube status
● vtube.service - cloudflared
   Loaded: loaded (/etc/systemd/system/vtube.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-10-22 22:37:30 CST; 59min ago
 Main PID: 2234 (cloudflared)
    Tasks: 9 (limit: 2155)
   CGroup: /system.slice/vtube.service
           └─2234 /home/XXX/bin/cloudflared --no-autoupdate --config /www/tunnel-vtube.yml tunnel run

....................