卡顿鸣 發表於 2025-8-21 15:56:00

Cloudflare Tunnel 内网穿透工具(Windows + 自定义域名)

<p><span style="font-size: 18px">适用于在 Windows 系统下,使用 Cloudflare Tunnel 将本地服务(如:http://localhost:8090)通过域名(如:web.xxxx.com)暴露到公网,无需公网 IP。</span></p>
<h1>📁 目录结构推荐</h1>
<pre class="language-yaml highlighter-hljs"><code>D:\cloudflared\
├── cloudflared.exe                      # 主程序
├── config.yml                           # 配置文件
├── cert.pem                           # 登录认证凭证
├── f1a3d2c4-xxxx-yyyy-zzzz-abcdefghijkl.json   # Tunnel 凭证文件</code></pre>
<h1>🧰 一、准备工作</h1>
<h2>1.1 注册并登录 Cloudflare</h2>
<ol>
<li>访问官网:https://dash.cloudflare.com/ </li>
<li>注册账户并登录。</li>
</ol>
<h2>1.2 添加域名到 Cloudflare(以&nbsp;<code>xxxx.com</code>&nbsp;为例)</h2>
<ol>
<li>登录 Cloudflare 后点击“添加站点”</li>
<li>输入你的域名 xxxx.com</li>
<li>选择免费套餐(Free)</li>
<li>Cloudflare 会提供两个 NS 服务器地址</li>
<li>前往你域名的注册商(如阿里云、腾讯云、华为云等),修改 DNS 为上述 NS 地址</li>
<li>等待 DNS 生效(通常几分钟到几小时)。</li>
</ol>
<h1>💾 二、下载并配置 cloudflared</h1>
<h2>2.1 下载 cloudflared.exe</h2>
<ol>
<li>访问:https://github.com/cloudflare/cloudflared/releases/</li>
<li>下载适用于 Windows 的 cloudflared-windows-amd64.exe</li>
<li>重命名为 cloudflared.exe</li>
<li>建议统一存放在:D:\cloudflared\</li>
</ol>
<blockquote>
<p>💡 所有配置文件都统一放在 D:\cloudflared\ 目录下,方便维护。</p>
</blockquote>
<h1>🔐 三、登录 Cloudflare 并授权</h1>
<h2>3.1 登录 cloudflare</h2>
<pre class="language-bash highlighter-hljs"><code>D:
cd D:\cloudflared
cloudflared.exe login</code></pre>
<ul>
<li>浏览器会自动打开授权页面</li>
<li>选择你的域名 xxxx.com 并点击授权</li>
<li>Cloudflare 会自动下载凭证文件(默认是下载到用户目录) C:\Users\你的用户名.cloudflared</li>
</ul>
<h2>3.2 拷贝 cert.pem 到指定目录</h2>
<p>将默认下载在:</p>
<pre class="language-yaml highlighter-hljs"><code>C:\Users\你的用户名\.cloudflared\cert.pem</code></pre>
<p>拷贝到:</p>
<pre class="language-yaml highlighter-hljs"><code>D:\cloudflared\cert.pem</code></pre>
<h1>🚇 四、创建 Tunnel 隧道</h1>
<h2>4.1:创建隧道</h2>
<p>在命令行中继续执行:</p>
<pre class="language-yaml highlighter-hljs"><code>cloudflared.exe tunnel create mytunnel</code></pre>
<p>创建成功后,将生成一个 UUID 凭证文件(如:f1a3d2c4-xxxx-yyyy-zzzz-abcdefghijkl.json)</p>
<ul>
<li>输出一条日志,提示 UUID,例如:</li>
</ul>
<pre class="language-yaml highlighter-hljs"><code>Created tunnel mytunnel with id: f1a3d2c4-xxxx-yyyy-zzzz-abcdefghijkl</code></pre>
<p>文件默认生成在:</p>
<pre class="language-yaml highlighter-hljs"><code>C:\Users\你的用户名\.cloudflared\</code></pre>
<p>请手动复制或移动该文件到 D:\cloudflared\ 目录下,并重命名为一致的名称(如:mytunnel.json)</p>
<ul>
<li>⚠️ 如果提示 “tunnel already exists”,请:</li>
<li>使用已有 tunnel(跳过此步),或 删除 tunnel 后重新创建:</li>
</ul>
<pre class="language-bash highlighter-hljs"><code>cloudflared.exe tunnel delete mytunnel</code></pre>
<h1>📁 五、配置 config.yml</h1>
<p>在 D:\cloudflared\ 目录下创建 config.yml 文件:</p>
<pre class="language-yaml highlighter-hljs"><code>tunnel: mytunnel
credentials-file: D:\cloudflared\mytunnel.json

ingress:
- hostname: web.xxxx.com   # 在cloudflared网站添加的域名(可以是子域名或者是顶级域名)
service: http://localhost:8090 # 本地服务的地址
- service: http_status:404</code></pre>
<p>说明:</p>
<ul>
<li>tunnel: 对应你创建的 tunnel 名称</li>
<li>credentials-file: 必须是你保存凭证文件的路径(和 cloudflared.exe 同级)</li>
<li>hostname: 你在 Cloudflare 上设置的子域名</li>
<li>service: 你本地服务的地址(根据实际情况修改)</li>
</ul>
<h1>🌐 六、将域名绑定到 Tunnel</h1>
<p>使用以下命令将你的子域名绑定到 tunnel 上::</p>
<pre class="language-bash highlighter-hljs"><code>cloudflared.exe tunnel route dns mytunnel web.xxxx.com</code></pre>
<p>Cloudflare 会自动添加一条 DNS 记录,将 web.xxxx.com 指向 Cloudflare Tunnel。<br>成功后 Cloudflare 会自动添加一条 DNS 记录,将该子域名指向该 tunnel。</p>
<h1>🚀 七、运行 Tunnel</h1>
<h2>方式一:手动运行</h2>
<pre class="language-bash highlighter-hljs"><code>cloudflared.exe tunnel --config D:\cloudflared\config.yml run</code></pre>
<h2>方式二:注册为系统服务(可选)</h2>
<pre class="language-bash highlighter-hljs"><code>cloudflared.exe service install --config D:\cloudflared\config.yml</code></pre>
<p>安装为服务后,系统启动时会自动运行 Cloudflare Tunnel。</p>
<h1>🧪 八、测试访问</h1>
<p>打开浏览器访问,若页面能正常显示本地服务内容,即配置成功 ✅</p>
<pre class="language-armasm highlighter-hljs"><code>http://web.xxxx.com</code></pre>
<h1>🧱 九、防火墙和本地服务排查(如遇问题)</h1>
<ul>
<li>
<p>✅ 确保本地服务已正常运行(如:8090 端口)</p>
</li>
<li>
<p>✅ Windows 防火墙未阻止 cloudflared.exe 出网</p>
</li>
<li>
<p>✅ 检查配置文件路径是否正确</p>
</li>
<li>
<p>✅ 如果你配置了<span class="words-blog hl-git-1" data-tit="其他" data-pretit="其他">其他</span>路径,请确保一致性</p>
</li>
</ul>
<h1>🧯 十、常见问题 FAQ</h1>
<table style="border-collapse: collapse; width: 38.0569%; height: 168px; border-style: dashed">
<thead style="border-collapse: collapse; background-color: rgba(194, 224, 244, 1); border-color: rgba(45, 194, 107, 1); border-style: solid">
<tr style="height: 21px">
<th style="width: 31.5498%; height: 21px">问题</th>
<th style="width: 63.0844%; height: 21px">说明</th>
</tr>
</thead>
<tbody>
<tr style="height: 42px">
<td style="width: 31.5498%; height: 42px">403 Forbidden</td>
<td style="width: 63.0844%; height: 42px">检查 config.yml 配置是否正确,是否执行了 tunnel route dns</td>
</tr>
<tr style="height: 21px">
<td style="width: 31.5498%; height: 21px">ERR_CONNECTION_REFUSED</td>
<td style="width: 63.0844%; height: 21px">本地服务未启动或监听地址不是 localhost</td>
</tr>
<tr style="height: 21px">
<td style="width: 31.5498%; height: 21px">Timeout</td>
<td style="width: 63.0844%; height: 21px">尝试使用 --protocol h2mux 参数启动</td>
</tr>
<tr style="height: 21px">
<td style="width: 31.5498%; height: 21px">登录授权跳转失败</td>
<td style="width: 63.0844%; height: 21px">复制授权链接手动在浏览器打开</td>
</tr>
<tr style="height: 21px">
<td style="width: 31.5498%; height: 21px">tunnel already exists</td>
<td style="width: 63.0844%; height: 21px">使用已有 tunnel 或删除后重建</td>
</tr>
<tr style="height: 21px">
<td style="width: 31.5498%; height: 21px">credentials file not found</td>
<td style="width: 63.0844%; height: 21px">确保凭证文件在指定位置并路径正确</td>
</tr>
</tbody>
</table>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:随手一只风,转载请注明原文链接:https://www.cnblogs.com/suishou/p/19050967</p><br><br>
来源:https://www.cnblogs.com/suishou/p/19050967
頁: [1]
查看完整版本: Cloudflare Tunnel 内网穿透工具(Windows + 自定义域名)