cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

一、前言

cloudflare argo tunnel是什么?

Cloudflare Tunnel 为您提供了一种无需公共 IP 地址即可将资源连接到 Cloudflare 的安全方式。

使用 Tunnel,您不会将流量发送到外部 IP — 相反,您的基础设施中的一个轻量级守护进程( cloudflared) 创建到 Cloudflare 边缘的仅出站连接。

Cloudflare Tunnel 可以将 HTTP Web 服务器、 SSH 服务器  远程桌面 和其他协议安全地连接到 Cloudflare。

这样,您的源站就可以通过 Cloudflare 提供流量,而不会受到绕过 Cloudflare 的攻击。

Cloudflared 在您的资源和 Cloudflare 边缘之间建立出站连接(隧道)。

在同一个隧道内,你可以运行尽可能多的 cloudflared根据需要处理(连接器)。

这些进程将建立与 Cloudflare 边缘的连接,并将流量发送到最近的 Cloudflare 数据中心。

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

额,你说你看不懂?

上面一堆东西,说人话就是人如其名,cloudflare tunnel就是一个内网穿透工具,类似于国内的花生壳这种

但是比它好用的多

二、为什么要用cloudflare tunnel

听我跟你讲,这个好处可太多太多

  1. cloudflare tunnel是一个内网穿透工具,它不需要你的服务器暴露端口到公网,也不需要你的服务器有公网ip地址,只要你的设备能联网就能启动一个网站,最关键是完全免费不要钱!带宽看你服务器带宽,和其他内网穿透工具不一样的是,用它搭建的服务自动接入cloudflare的防御,家宽建站不是梦,而且没有任何限制。(除了TOS,自己去看
  2. 默认情况下,你无法决定cloudflare cdn回源方式,但是你用了tunnel穿透网站的方式,可以指定http2/QUIC/http2 mux等回源方式
  3. 反正都是面向cf建站,argo tunnel会自动给你申请和续期ssl证书,这方面续期和申请的工作可以无视
  4. 迁移和维护非常方便,启动也很简单
  5. 不怕别人扫描你的网站,由于没有屏蔽非CF的ip访问,导致源站暴露,面临被攻击的风险
  6. 如果你是面向cloudflare建站的,argo tunnel的性能比nginx要好,它高带宽和高并发的情况下,明显argo tunnel占用更低,不信你可以去尝试一下1Gbps以上的传输
  7. 做站不会占用任何公网端口,你可以同时在一台服务器上同时启动多个网站,不用再考虑端口占用了
  8. 启动多个实例,宕机自动切换

三、有什么缺点

我觉得没什么缺点,让我评价都是优点,没有不好的地方

硬要说的话,就是你既然用的是cloudflare的内网穿透工具,那么必须把网站接入它的CDN才可以

四、实验环境

您需要的东西:

  1. cloudflare账户一枚
  2. 托管在cloudflare一个域名
  3. 一个完整的大脑
  4. 一双没有缺陷的眼睛
  5. 一张会提问的嘴巴

五、Start a tunnel with cli

看完这小节,你可以了解如何用cli使用cloudflare argo tunnel

这不是我们的重点,我们重点是后面docker那部分

1、下载cloudflared工具,连接到你的cloudflare账户

这个不推荐放到服务器上获取储存凭证,因为服务器有被黑的风险,你可以放到本地windwos、linux都行

根据你的架构选择release,这里用x86-64的架构举例

curl -o /usr/bin/cloudflared -L "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64" #从github下载
 
chmod +x /usr/bin/cloudflared #给可执行权限

如图:

 

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

然后登陆连接你的cloudflare账户,需要使用cloudflare的zero trust

登录时随便选一个托管在cf的域名授权就行了

cloudflared login
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

如果说你要迁移,就把cert.pem这个配置文件下载到其他机器相同的路径就行了,用其他域名也是,不需要重复授权,只需要这一次就能创建隧道

2. 创建一个隧道

非常简单

cloudflared tunnel create <隧道名字>

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

 

配置文件路径在/root/.cloudflared/<id>.json

这个意思就是,你现在用你获得你账户的凭证,创建了一个隧道,你可以用<id>.json 去使用这个隧道

3. 管理

 
COMMANDS:
   login    Generate a configuration file with your login details
   create   Create a new tunnel with given name
   route    Define which traffic routed from Cloudflare edge to this tunnel: requests to a DNS hostname, to a Cloudflare Load Balancer, or traffic originating from Cloudflare WARP clients
   vnet     Configure and query virtual networks to manage private IP routes with overlapping IPs.
   run      Proxy a local web server by running the given tunnel
   list     List existing tunnels
   info     List details about the active connectors for a tunnel
   delete   Delete existing tunnel by UUID or name
   cleanup  Cleanup tunnel connections
   token    Fetch the credentials token for an existing tunnel (by name or UUID) that allows to run it
   help, h  Shows a list of commands or help for one command

额,这里不用多说吧,你运行cloudflared help直接看就行了

 

值得注意的就是几个login create route run list info delete token

这些光看这个单词的意思就能猜出是干什么的,不用我去详细介绍

4. 如何用cli去做内网穿透

上面说了哈,我们创建隧道的这个过程是可以在你内网环境下做的,不需要服务器

那么我们如何其他地方使用穿透功能呢?

先获取你创建隧道的这个token

cloudflared tunnel token <隧道名称>
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

你在这里获得token意味着其他人有下面这串神秘代码就能使用你的隧道,请谨慎保管

什么,你说这样不安全?

我当然知道啦,下面会介绍其他连接方式,先慢慢来搞懂cli再说

你复制这串神秘代码后,在想要使用隧道的地方这样输入命令

cloudflared tunnel --no-autoupdate run --token <上面那一串神秘代码> --url http://127.0.0.1:8080

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

 

出现这个说明连接成功

这样我们的在此8080端口上内网服务,就会被穿透到cf哪里(支持设置proxy穿透隧道,自己help查看

测试网址 https://<id>.cfargotunnel.com ,你浏览器访问一下就知道了

那么如何绑定到我自己的域名呢

也非常简单,在dashboard创建一个cname并开启小云朵就行了,这样cf就会自动把你这个域名绑定到这个隧道上面,以后你只需要访问这个域名就行了

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

当然cli部分你权当介绍看看就行了,我们的重心不是这里,它只让你对它有个简单的了解,下面的配合docker使用才是真正的方便

六、Start with docker-compose

这里用cloudflared docker 启动一个wordpress做例子,其他也是同理,在下面给出几个常用服务的配置样例

1、安装docker-compose

apt/yum install -y docker-compose 

如果没有装过docker,包管理器会自动给你装上docker,毕竟是依赖的一部分

 

2、文件夹结构

其实没必要用文件夹,但是为了我们以后管理方便,这里还是每个容器使用一个文件夹吧

blog
├── html
├── mariadb_data
├── cloudflared
│   ├── config.yaml
│   └── tunnel
│       └── id.json
└── docker-compose.yml

解释一下

 

html以后放wordpress的文件,mariadb_data放的是数据库文件,这俩都不需要创建

你创建的是cloudflared/文件夹和它的子目录tunnel/以及创建编辑config.yaml,这个是tunnel的配置文件,指定配置

3. docker-compose.yml

这个你只需要编辑数据库那部分就行了,修改成你的密码,其他保持原样,然后保存

version: "3"
services:
  argo_tunnel:
    image: cloudflare/cloudflared:latest
    volumes:      
      - ./cloudflared:/etc/cloudflared
    restart: always
    command: 'tunnel --config /etc/cloudflared/config.yaml run'
    user: root
    depends_on:
      - wp
  
  wp:
    image: wordpress:latest
    restart: always
    volumes:
      - ./html/:/var/www/html
    depends_on:
      - mariadb_wp
 
  mariadb_wp:
    image: mariadb:latest
    environment:
       MYSQL_ROOT_PASSWORD: 123456789 #数据库root密码
       MYSQL_DATABASE: wordpress #数据库名称
       MYSQL_USER: root #建议用root
       MYSQL_PASSWORD: Emptysuns0. #数据库密码
    volumes:
      - ./mariadb_data:/var/lib/mysql
    command: mysqld  --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    privileged: true

 

 

4. 配置cloudflared config.yaml

其实也很简单

首先把上面创建隧道生成的那个<id>.json 下载下来,上传到服务器文件夹里./cloudflared/tunnel/

记得改名成id.json

config.yaml

tunnel: <id>
credentials-file: /etc/cloudflared/tunnel/id.json
protocol: quic
ingress:
- hostname: blog_tunnel.test.xyz
  service: http://wp:80
- service: http_status:404

解释一下

 

tunnel:<id>就是你隧道的那个id,粘贴上,不需要双引号比如123156465-456adwada456dwad-dawdawd

credentials-file: 不需要你动

protocol: 可选项<quic/http2/h2mux>, 默认quic,使用quic回源时,对性能占用往往很高,如果没有很变态的延迟要求的人,建议用http2和h2mux,h2mux你可以理解为多通道的意思,实测这俩区别不大(如果你禁用了udp也是不能用quic的注意啦)

ingress: 相当于入站规则 hostname是绑定到这个隧道的cf托管域名,这个tunnel不会自动添加,你需要cli那一步去dashboard手动添加 最后一个service: http_status:404,这个必须得加,有个收尾规则,你可以吧404改成666或者123,那无所谓

如果说你要用SaaS接入cf,则是

tunnel: <id>
credentials-file: /etc/cloudflared/tunnel/id.json
protocol: quic
ingress:
- hostname: blog_tunnel.test.xyz
  service: http://wp:80
- hostname: blog_tunnel.test.com #你的SaaS接入的域名
  service: http://wp:80
- service: http_status:404

这也是为什么要用配置文件启动tunnel的原因,因为用命令行指定SaaS域名太麻烦了

 

5. docker-compose管理

启动

docker-compose up -d

查看日志

 

docker-compose logs -f --tail=30

停止

 

docker-compose down

迁移

 

把这个blog/文件夹打包直接拷走到其他服务器重启启动就行了,因为自动证书,所以啥也不用管就能启动,你就说方便不方便ba

6. 启动博客

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

额,没了就这点,简单吧哈哈哈哈,是不是比你从零开始编译环境方便?

七、其他服务示例

bitwarden

version: "3"
services:
 
  argo_tunnel:
    image: cloudflare/cloudflared:latest
    volumes:      
      - ./cloudflared:/etc/cloudflared
    restart: always
    command: 'tunnel --config /etc/cloudflared/config.yaml run'
    user: root
    depends_on:
      - bw
 
  bw:
    image: vaultwarden/server:latest
    restart: always
    environment:
      DOMAIN: 'https://bwh.test.com/' #你cf绑定的自己域名
      SIGNUPS_ALLOWED: 'false'
      WEBSOCKET_ENABLED: 'true'
      SMTP_HOST: smtp.office365.com #smtp设置
      SMTP_FROM: admin@test.com #邮箱
      SMTP_PORT: '587'
      SMTP_SSL: 'true'
      SMTP_USERNAME: admin@test.com
      SMTP_PASSWORD: "TTTTTEST" #密码
      SMTP_AUTH_MECHANISM: "Login"
    volumes:
      - ./b/data:/data

 

 

alist

version: "3"
services:
  argo_tunnel:
    image: cloudflare/cloudflared
    volumes:      
      - ./cloudflared:/etc/cloudflared
    restart: always
    command: 'tunnel --config /etc/cloudflared/config.yaml run'
    user: root
    depends_on:
      - alistv3
  
  alistv3:
    image: xhofe/alist
    volumes:
      - ./alistv3/data:/opt/alist/data

 

 

emby

version: "3"
services:
  argo_tunnel:
    image: cloudflare/cloudflared
    volumes:      
      - ./cloudflared:/etc/cloudflared
    restart: always
    command: 'tunnel --config /etc/cloudflared/config.yaml run'
    user: root
    depends_on:
      - emby
 
  emby:
    image: lscr.io/linuxserver/emby:latest
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/London
    volumes:
      - ./emby_config:/config
      - ./rclone/hentai_data/anime:/data/hanime
    restart: always

 

 

其他服务实例欢迎放到评论区,供给大家参考

八、你知道吗?

1.tunnel不止可以穿透http服务

它可以穿透所有TCP协议,使用方法也很简单

把config.yaml修改一下就行了,比如

service: tcp://127.0.0.1:8888

 

 

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

2. 可以在cloudflare zero trust面板管理你的tunnel

这个面板也可以添加隧道不只是用命令行创建

cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客

3. 用一个隧道可以同时被其他机器启动

cf会优先选择第一时间启动的机器作为主节点,当主节点down掉后会自动切换的其他节点,这个是按照启动的顺序决定的

4. tunnel一个帐号有创建的数量限制

但是我记得数量限制一个人是用不完的,不用担心,具体数量忘了

九、常见问题

TG群: https://t.me/imoeq_group

上一个群管理员帐号被杜叔叔给封了,无法解封,所以新创建一个

创建个群的目的是为了,不给一些小白解决问题

短短一篇博文是无法解释清楚所有问题的,如果你有其他问题,请加群问群友,或者发邮件联系作者

1.我可以用家宽建站吗?

为什么不能呢?

关于家宽建站这方面,argo tunnel可以在连接时指定代理地址,所以只能你的梯子还能用,你还能访问谷歌,那么就能使用tunnel穿透

具体的配置我没有直接在博客里放出来,你可以去cloudflare argo tunnel docs里面去查询连接时代理配置项

--proxy-address value                        Listen address for the proxy. (default: "127.0.0.1") [$TUNNEL_PROXY_ADDRESS]
 --proxy-port value                           Listen port for the proxy. (default: 0) [$TUNNEL_PROXY_PORT]

就是在配置时启动时指定socks5地址和端口,比如

argo_tunnel:
    image: cloudflare/cloudflared:latest
    volumes:      
      - ./cloudflared:/etc/cloudflared
    restart: always
    command: 'tunnel --config /etc/cloudflared/config.yaml --proxy-address 127.0.0.1 --proxy-port 10808 run'
    user: root
    depends_on:
      - bw

 

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
cloudflare tunnel 详细介绍 —— 面向cloudflare的网站搭建,1分钟快速启动一个有ssl证书的wordpress博客
一、前言 cloudflare argo tunnel是什么? Cloudflare Tunnel 为您提供了一种无需公共 IP 地址即可将资源连接到 Cloudflare 的安全方式。 使用 Tunnel,您不会……
<<上一篇
下一篇>>