本文将指导您如何安装 Cloudflare 的隧道客户端(cloudflared),并通过快速隧道(Quick Tunnel)将本地 Web 服务器暴露到公网。
安装 Cloudflare 客户端(cloudflared)
首先,您需要根据您的操作系统安装 Cloudflare Tunnel 的客户端 cloudflared。
下载地址:Cloudflare 客户端下载页面
架构确认(以 Linux 为例)
在下载和安装前,请确认您的系统架构,以选择正确的安装包。运行以下命令查看架构:
uname -m
| 返回值 | 常见架构说明 |
|---|---|
x86_64 |
AMD64/Intel 64(也称 x64) |
i686 或 i386 |
32 位 x86 架构 |
aarch64 |
64 位 ARM 架构 |
armv7l |
32 位 ARM 架构 |
安装示例(Debian/Ubuntu)
以适用于基于 Debian 的系统(如 Ubuntu)的 .deb 包为例,下载后运行以下命令安装:
dpkg -i <FILE>.deb
安装完成后,您就可以使用 cloudflared 命令来配置隧道了。
通过 Cloudflare 暴露端口(快速隧道)
使用 Cloudflare 的快速隧道(Quick Tunnel)功能,可以快速将本地的 Web 服务(例如运行在 80 端口的 Apache2)临时暴露到公网。
注意:这种快速隧道没有正常运行时间保证,建议用于测试和实验。如需用于生产环境,请使用命名隧道(Named Tunnel)。
运行以下命令,将本地 http://localhost:80 暴露:
cloudflared tunnel --url http://localhost:80
运行后,您将看到类似如下的输出信息:
2025-06-17T14:52:05Z INF Thank you for trying Cloudflare Tunnel...
2025-06-17T14:52:05Z INF Requesting new quick Tunnel on trycloudflare.com...
2025-06-17T14:52:11Z INF +--------------------------------------------------------------------------------------------+
2025-06-17T14:52:11Z INF | Your quick Tunnel has been created! Visit it at (it may take some time to be reachable): |
2025-06-17T14:52:11Z INF | https://recording-phpbb-webmaster-cf.trycloudflare.com |
2025-06-17T14:52:11Z INF +--------------------------------------------------------------------------------------------+
...
获取公网访问 URL:
请记录输出中提供的隧道 URL,例如:https://recording-phpbb-webmaster-cf.trycloudflare.com。
此 URL 是 Cloudflare 为您的本地 Apache 服务器生成的临时公网地址。外部用户通过此 URL 访问,等同于访问您本地的 http://localhost:80。
网页版介绍:使用 Cloudflare Zero Trust 仪表板
除了使用命令行工具创建临时隧道,您还可以使用 Cloudflare Zero Trust 仪表板来创建永久的命名隧道(Named Tunnel),并将其与您自己的域名关联。这种方式更适合生产环境或需要长期运行的服务。
1. 登录 Zero Trust 仪表板
访问 Cloudflare Zero Trust 仪表板 并登录您的 Cloudflare 账户。
2. 创建隧道
导航到 Access > Tunnels,点击 Create a Tunnel。
3. 配置命名隧道
- 命名隧道:为您的隧道设置一个易于识别的名称(例如
my-web-tunnel)。 - 选择环境和连接器:
- 环境:选择运行
cloudflared的环境。 - 连接器:系统会提示您选择或安装连接器(即
cloudflared客户端)。您需要先在本地服务器上安装cloudflared并将其与此命名隧道关联起来。
- 环境:选择运行
- 配置路由:
- 在 Public Hostnames 部分,定义您希望通过哪个域名和路径来访问本地服务。
- Domain (子域名):选择您的 Cloudflare 域名。
- Service (服务):指定本地服务的协议和地址,例如
http://localhost:80。
配置完成后,Cloudflare 会为您生成一个长期稳定的公网 URL,并通过您自己的域名提供服务,且带有 Zero Trust 的安全保护功能。
网页版使用教程可参考这篇文章:https://www.haoyep.com/posts/access-intranet-ports-via-cloudflare-tunnel/
原文 https://blog.csdn.net/2301_79518550/article/details/150938354