本地管理的 Tunnel 作为您机器上的 cloudflared 实例运行。您可以通过修改命令行参数或编辑 Tunnel 配置文件来配置 cloudflared 属性。
如果您通过 cloudflared 连接单个服务,CLI 提供了处理配置的快捷方式。如果您连接多个服务并且需要为特定源站配置属性或例外情况,则 Tunnel 配置文件非常有用。在配置文件中,您可以为您的 cloudflared 实例定义顶级属性,以及源站专属属性。有关配置选项的完整列表,请在终端中输入 cloudflared tunnel help。
在没有配置文件的情况下,cloudflared 将通过端口 8080 代理出站流量。
私有网络的文件结构
如果您正在向运行 Cloudflare One Client 的最终用户 公开私有网络 ,您需要添加 warp-routing 键并将其设置为 true:
tunnel: <Tunnel-UUID>
credentials-file: /path/<Tunnel-UUID>.json
warp-routing:
enabled: true如果您要将本地服务公开到互联网,可以为每个服务分配一个公共主机名:
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 接收到传入请求时,它会自上而下评估每条入站规则,以找到匹配该请求的规则。规则可以匹配传入请求的主机名或路径,或两者都匹配。如果规则未指定主机名,则会匹配所有主机名。如果规则未指定路径,则会匹配所有路径。
最后一条入站规则必须是匹配所有流量的兜底规则。
下面是一个指定了多条规则的示例配置文件:
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.comUsing rules from /usr/local/etc/cloudflared/config.yml
Matched rule #3
hostname: *.example.com
service: https://localhost:8000对给定 Tunnel 的配置文件进行更改时,我们建议依赖 cloudflared 副本以最小的停机时间传播新配置。
- 让一个
cloudflared实例运行原始版本的配置文件。 - 启动一个运行更新版本配置文件的
cloudflared副本。 - 等待该副本完全运行并可用。
- 停止第一个
cloudflared实例。
您的 cloudflared 现在将运行更新版本的配置文件。