跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

配置文件

最后更新 查看 MarkdownAgent 设置

本地管理的隧道以 cloudflared 实例的形式在你的设备上运行。你可以修改命令行参数,或编辑隧道配置文件,来配置 cloudflared 的属性。

如果只通过 cloudflared 连接一项服务,CLI 可以让你快速完成配置。如果连接多项服务,并需要为特定源站配置属性或例外规则,隧道配置文件会更合适。你可以在配置文件中定义 cloudflared 实例的顶层属性,以及源站专属属性。要查看完整的配置选项列表,请在终端运行 cloudflared tunnel help。

在没有配置文件的情况下,cloudflared 将通过端口 8080 代理出站流量。

私有网络的文件结构

如果要向运行 Cloudflare One Client 的最终用户公开私有网络,配置文件只需要隧道 UUID 和凭据文件路径:

tunnel: <Tunnel-UUID>
credentials-file: /path/<Tunnel-UUID>.json

已发布应用程序的文件结构

如果您要将本地服务公开到互联网,可以为每个服务分配一个公共主机名:

tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json

ingress:
  - hostname: gitlab.widgetcorp.tech
    service: http://localhost:80
  - hostname: gitlab-ssh.widgetcorp.tech
    service: ssh://localhost:22
  - service: http_status:404

包含入站规则(ingress rules)的配置文件必须始终在文件末尾包含一个兜底规则(catch-all rule)。在本例中,当请求不匹配之前的任何主机名时,cloudflared 将返回 404 状态码。

流量匹配方式

在 cloudflared 2026.10.0 及更高版本中,你可以在匹配入口规则前规范化 URL。这可以提高需要 Cloudflare Access JWT 验证的规则的安全性。

规范化会按照 RFC 3986 ↗ 移除点段(. 和 ..)。它还会将反斜杠转换为正斜杠,并合并重复的斜杠。

在版本 2026.10.0 中,规范化默认关闭。要将其启用,请在 run 之前设置 --disable-path-normalization=false。

请在计划的默认行为变更前测试基于路径的路由。不含需要 Access 验证规则的配置不受影响。

以下示例适用于已启用规范化,且隧道包含需要 Access 验证的规则的情况。传入路径按 cloudflared 接收时的形式显示:

传入路径 用于入口匹配的路径 发送到源站的路径
/hello/../path /path /hello/../path
/hello//path /hello/path /hello//path
/hello\path /hello/path /hello\path

下面是一个指定了多条规则的示例配置文件:

tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json

ingress:
  # Rules map traffic from a hostname to a local service:
  - hostname: example.com
    service: https://localhost:8000
  # Rules can match the request's path to a regular expression:
  - hostname: static.example.com
    path: \.(jpg|png|css|js)$
    service: https://localhost:8001
  # Rules can match the request's hostname to a wildcard character:
  - hostname: "*.example.com"
    service: https://localhost:8002
  # An example of a catch-all rule:
  - service: https://localhost:8003

通配符

您可以使用通配符将流量匹配到多个子域。例如,如果将 hostname 键设置为 *.example.com,则 alpha.example.com 和 beta.example.com 都会将流量路由到您的源站。cloudflared 不支持在主机名中间使用通配符,例如 test.*.example.com。

您还可以为 path 键输入正则表达式。例如,如果 hostname 为 static.example.com 且 path 为 \.(jpg|png|css|js)$,则匹配的 URL 可能包括 https://static.example.com/data.js、http://static.example.com/images/photo.jpg 等。Cloudflare 使用 Go syntax 包 ↗解析路径正则表达式。

服务

除 HTTP 外,cloudflared 还支持 SSH、RDP、任意 TCP 服务以及 Unix 套接字等协议。你也可以将流量路由到内置的 hello_world 测试服务器,或返回 HTTP 状态码。有关支持的服务类型完整列表,请参阅已发布应用程序支持的协议。

tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json

ingress:
  # Example of a request over TCP:
  - hostname: example.com
    service: tcp://localhost:8000
  # Example of an HTTP request over a Unix socket:
  - hostname: staging.example.com
    service: unix:/home/production/echo.sock
  # Example of a request mapping to the Hello World test server:
  - hostname: test.example.com
    service: hello_world
  # Example of a rule responding to traffic with an HTTP status:
  - service: http_status:404

源站配置

如果需要通过一个 cloudflared 实例代理到多个源站,可以在入口规则中指定配置选项,定义 cloudflared 如何向各项服务发送请求。

在以下示例中,顶级配置 connectTimeout: 30s 为该 cloudflared 实例内的所有服务设置了 30 秒的连接超时。接着,对于 service: localhost:8002 的入站规则通过将该服务的 connectTimeout 设置为 10s,配置了对顶级配置的例外。30 秒的连接超时仍适用于所有其他服务。

tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json
originRequest: # Top-level configuration
  connectTimeout: 30s

ingress:
  # The localhost:8000 service inherits all root-level configuration.
  # In other words, it will use a connectTimeout of 30 seconds.
  - hostname: example.com
    service: localhost:8000
  - hostname: example2.com
    service: localhost:8001
  # The localhost:8002 service overrides some root-level config.
  - service: localhost:8002
    originRequest:
      connectTimeout: 10s
      disableChunkedEncoding: true
  # Some built-in services such as `http_status` do not use any configuration.
  # The service below will simply respond with HTTP 404.
  - service: http_status:404

验证入站规则

要验证配置文件中的入站规则,请运行:

cloudflared tunnel ingress validate

这将确保配置文件中指定的入站规则集是有效的。

测试入站规则

要验证 cloudflared 是否会将正确的流量代理到正确的本地服务,请使用 cloudflared tunnel ingress rule。这会根据从第一条到最后一条规则检查 URL,并显示第一条匹配的规则。例如:

cloudflared tunnel ingress rule https://foo.example.com
Using rules from /usr/local/etc/cloudflared/config.yml
Matched rule #3
    hostname: *.example.com
    service: https://localhost:8000

更新配置文件

更改某条隧道的配置文件时,建议使用cloudflared 副本传播新配置,以尽量缩短停机时间。

  1. 让一个 cloudflared 实例运行原始版本的配置文件。
  2. 启动一个运行更新版本配置文件的 cloudflared 副本。
  3. 等待该副本完全运行并可用。
  4. 停止第一个 cloudflared 实例。

您的 cloudflared 现在将运行更新版本的配置文件。

这篇文档对您有帮助吗?