在 5 分钟内创建 Cloudflare Tunnel 并发布您的第一个应用程序。
- 一个 Cloudflare 账户 ↗
- Cloudflare 上的一个域(发布应用程序必需)
- 一台可以访问互联网的服务器或虚拟机,您将在其中安装
cloudflared
要创建新的 Cloudflare Tunnel:
-
在 Cloudflare 仪表板 ↗中,转到 Networking(网络) > Tunnels(隧道)。
Go to Tunnels ↗ -
选择 Create Tunnel(创建隧道)。
-
输入隧道的名称(例如
production-web或staging-api)。 -
选择 Create Tunnel(创建隧道)。
-
在 Setup Environment(设置环境) 下,选择服务器的操作系统和架构。
-
复制 Install and Run(安装并运行) 下显示的安装命令,并在服务器上的终端中运行它们。
-
隧道连接后,选择 Continue(继续)。
您的隧道应显示在 Tunnels(隧道) 页面上,状态为 Healthy。
-
创建 API 令牌,具有以下权限:
类型 项目 权限 Account Cloudflare Tunnel Edit Zone DNS Edit -
创建隧道:
At least one of the following token permissions is required:Required API token permissions
Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
Create a Cloudflare Tunnelbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "name": "api-tunnel", "config_src": "cloudflare" }' -
复制响应中的
id和token值。配置和运行隧道需要它们。
要使应用程序可从互联网访问,请向您的隧道添加已发布应用程序路由。隧道路由将公共主机名映射到本地服务。
在 Cloudflare 仪表板 ↗中,转到 Networking(网络) > Tunnels(隧道) 并选择您的隧道。 Go to Tunnels ↗
-
在 Routes(路由) 下,选择 Add route(添加路由)。
-
选择 Published application(已发布的应用程序)。
-
在 Hostname(主机名) 下,输入子域并从下拉菜单中选择一个域。
-
对于 Service URL,输入应用程序的本地地址和端口。
例如,如果您的 Web 服务器运行在与
cloudflared相同的计算机上:- 端口
80上的 HTTP:http://localhost:80 - 端口
443上的 HTTPS:https://localhost:443
如果您的 Web 服务器运行在另一台计算机上:
http://192.0.2.1:80 - 端口
-
选择 Add route(添加路由)。
-
配置隧道的入口规则:
At least one of the following token permissions is required:Required API token permissions
Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
Put configurationbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/configurations" \ --request PUT \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "config": { "ingress": [ { "hostname": "app.example.com", "service": "http://localhost:80", "originRequest": {} }, { "service": "http_status:404" } ] } }'您的入口规则必须在末尾包含一条包罗万象的规则。在此示例中,当请求与任何主机名都不匹配时,
cloudflared将响应 404 状态代码。 -
为您的应用程序创建 DNS 记录:
At least one of the following token permissions is required:Required API token permissions
DNS Write
Create DNS Recordbash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "type": "CNAME", "proxied": true, "name": "app.example.com", "content": "<TUNNEL_ID>.cfargotunnel.com" }' -
在服务器上安装
cloudflared并使用在创建隧道中获得的token运行隧道:-
下载并安装 ↗
cloudflared。 -
运行以下命令:
sudo cloudflared service install <TUNNEL_TOKEN>
-
下载并安装
cloudflared。 -
以管理员身份打开命令提示符。
-
运行以下命令:
cloudflared.exe service install <TUNNEL_TOKEN>
-
下载并安装
cloudflared。 -
打开终端窗口并运行以下命令:
sudo cloudflared service install <TUNNEL_TOKEN>
-
打开终端窗口。
-
运行以下命令:
docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token <TUNNEL_TOKEN>
-
您的应用程序现已在您配置的主机名上线。Cloudflare 会自动代理通过其网络的流量,应用 CDN 缓存、WAF 和 DDoS 保护。
对于本地开发,您可以在没有 Cloudflare 账户的情况下立即公开 localhost:
cloudflared tunnel --url http://localhost:8080这会生成一个随机的 trycloudflare.com 子域,用于将流量代理到本地服务器。快速隧道仅用于测试 — 它们的并发请求限制为 200,并且不支持服务器发送事件 (SSE)。
对于生产环境,请改用创建隧道。