本指南将引导您创建您的第一个 Workers VPC 服务,允许您的 Worker 访问私有网络中的资源。
您将创建一个 Workers 应用程序,在私有网络中创建一个 Tunnel 以将其连接到 Cloudflare,然后为您希望从 Workers 访问的私有网络上的服务配置 VPC 服务。
在开始之前,请确保您已完成以下操作:
- 注册 Cloudflare 账户 ↗。
- 安装
Node.js↗。
Node.js 版本管理器
使用 Volta ↗ 或 nvm ↗ 等 Node 版本管理器,以避免权限问题并切换 Node.js 版本。本指南后续将介绍的 Wrangler 需要 Node 版本 16.17.0 或更高。
此外,您还需要:
- 访问私有网络(您的本地网络、AWS VPC、Azure VNet、GCP VPC 或本地网络)
- Connectivity Directory Bind(连接目录绑定) 角色,以便从 Workers 绑定到现有的 VPC 服务。
- 或者,Connectivity Directory Admin(连接目录管理员) 角色,以创建 VPC 服务,并从 Workers 绑定到这些服务。
使用 Wrangler 创建一个新的 Worker 项目:
npm create cloudflare@latest -- workers-vpc-appyarn create cloudflare workers-vpc-apppnpm create cloudflare@latest workers-vpc-app进行设置时,请选择以下选项:
- 对于 What would you like to start with?,选择
Hello World example。 - 对于 Which template would you like to use?,选择
Worker only。 - 对于 Which language do you want to use?,选择
TypeScript。 - 对于 Do you want to use git for version control?,选择
Yes。 - 对于 Do you want to deploy your application?,选择
No(部署前我们还会做一些修改)。
导航到您的项目目录:
cd workers-vpc-appCloudflare Tunnel 可创建从私有网络到 Cloudflare 的安全连接。此隧道将允许 Workers 安全地访问您的私有资源。您可以在外部云中的虚拟机或容器上创建隧道,为了本教程,您甚至可以在本地桌面上创建隧道。
-
导航到 Workers VPC 仪表板 ↗ 并选择 Tunnels(隧道) 选项卡。
-
选择 Create(创建) 以创建一个新隧道。
-
输入您的隧道名称(例如,
workers-vpc-tunnel)并选择 Save tunnel(保存隧道)。 -
选择您的操作系统和架构。仪表板将为您的环境提供特定的安装说明。
-
按照提供的命令下载并安装
cloudflared,并使用您的唯一令牌执行服务安装命令。
仪表板将在您的隧道成功连接时进行确认。
连接隧道后,您需要确保它可以访问您希望 Workers 访问的服务。隧道应安装在可以访问您想要向 Workers VPC 公开的内部资源的机器上。在外部云中,这可能意味着配置访问控制列表(Access-Control-Lists)、安全组(Security Groups)或 VPC 防火墙规则,以确保隧道可以访问所需的服务。
既然您的隧道已在运行,请创建一个 Workers 可以用来访问您的内部资源的 VPC 服务:
-
导航到 Workers VPC 仪表板 ↗ 并选择 **VPC Services(VPC 服务)**选项卡。
-
选择 Create(创建) 以创建一个新的 VPC 服务。
-
为您的 VPC 服务输入一个 Service name(服务名称)(例如,
my-private-api)。 -
从 Tunnel(隧道) 下拉菜单中选择您的隧道,或者如果需要创建新隧道,请选择 Create Tunnel(创建隧道)。
-
输入您内部服务的 Host or IP address(主机或 IP 地址)(例如,
localhost、internal-api.company.local或10.0.1.50)。 -
配置 Ports(端口)。选择以下其一:
- Use default ports(使用默认端口) 使用标准 HTTP (80) 和 HTTPS (443) 的默认端口
- Provide port values(提供端口值) 以指定自定义的 HTTP 和 HTTPS 端口
-
配置 DNS Resolver(DNS 解析器)。选择以下其一:
- Use tunnel as resolver(使用隧道作为解析器) 使用隧道内置的 DNS 解析
- Custom resolver(自定义解析器) 并输入您的 DNS 解析器 IP(例如,
8.8.8.8)
-
选择 Create service(创建服务) 以创建您的 VPC 服务。
仪表板将显示您的新 VPC 服务以及唯一的服务 ID。请保存此服务 ID 以供下一步使用。
对于 HTTP 服务:
npx wrangler vpc service create my-private-api \
--type http \
--tunnel-id <YOUR_TUNNEL_ID> \
--hostname <YOUR_HOSTNAME>对于 TCP 服务(例如,PostgreSQL 数据库):
npx wrangler vpc service create my-database \
--type tcp \
--tcp-port 5432 \
--app-protocol postgresql \
--tunnel-id <YOUR_TUNNEL_ID> \
--ipv4 <YOUR_IPV4_ADDRESS>替换:
<YOUR_TUNNEL_ID>为您在第 2 步中获得的隧道 ID<YOUR_HOSTNAME>为您的内部服务主机名(例如,internal-api.company.local)<YOUR_IPV4_ADDRESS>为您服务的私有 IP 地址(例如,10.0.1.50)
您还可以:
- 通过将
--hostname <YOUR_HOSTNAME>替换为--ipv4 <YOUR_IPV4_ADDRESS>、--ipv6 <YOUR_IPV6_ADDRESS>,或者为双栈配置同时使用两者,来创建使用 IP 地址的服务 - 通过添加
--http-port <PORT>和/或--https-port <PORT>(例如,--http-port 8080 --https-port 8443)来指定 HTTP 服务的自定义端口 - 使用
--cert-verification-mode设置 TLS 证书验证模式(verify_full、verify_ca或disabled)
该命令将返回一个服务 ID。保存此 ID 以供下一步使用。
如果您遇到权限错误,请参阅所需角色。
将 VPC 服务绑定添加到您的 Wrangler 配置文件中:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "workers-vpc-app",
"main": "src/index.ts",
// Set this to today's date
"compatibility_date": "2026-08-17",
"vpc_services": [
{
"binding": "VPC_SERVICE",
"service_id": "<YOUR_SERVICE_ID>"
}
]
}"$schema" = "./node_modules/wrangler/config-schema.json"
name = "workers-vpc-app"
main = "src/index.ts"
# Set this to today's date
compatibility_date = "2026-08-17"
[[vpc_services]]
binding = "VPC_SERVICE"
service_id = "<YOUR_SERVICE_ID>"将 <YOUR_SERVICE_ID> 替换为您在第 3 步中获得的服务 ID。
更新您的 Worker 以使用 VPC 服务绑定。以下示例:
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = new URL(request.url);
// This is a simple proxy scenario.
// In this case, you will need to replace the URL with the proper protocol (http vs. https), hostname and port of the service.
// For example, this could be "http://localhost:1111", "http://192.0.0.1:3000", "https://my-internal-api.example.com"
const targetUrl = new URL(
`http://<ENTER_SERVICE_HOST>:<ENTER_SERVICE_PORT>${url.pathname}${url.search}`,
);
// Create new request with the target URL but preserve all other properties
const proxyRequest = new Request(targetUrl, {
method: request.method,
headers: request.headers,
body: request.body,
});
const response = await env.VPC_SERVICE.fetch(proxyRequest);
return response;
},
} satisfies ExportedHandler<Env>;在本地测试您的 Worker。您必须使用远程 VPC 服务,要么使用在您的 wrangler.jsonc 配置文件中配置的 Workers 远程绑定,要么使用 npx wrangler dev --remote:
npx wrangler dev访问 http://localhost:8787,测试您的 Worker 与私有网络的连接。
测试完成后,部署您的 Worker:
npx wrangler deploy您的 Worker 现已部署,并可通过 Cloudflare Tunnel 安全地访问您的私有网络资源。如果您遇到权限错误,请参阅所需角色。