> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/publish/custom-domain/setting-a-custom-subdirectory/configuring-a-subdirectory-with-cloudflare.md).

# 使用 Cloudflare 配置子目录

使用 Cloudflare 通过 /docs 子目录托管你的文档

{% stepper %}
{% step %}

#### 配置您的 GitBook 站点

在 GitBook 组织中，点击侧边栏中的文档站点名称，然后点击 **管理站点** 或打开 **设置** 选项卡。打开 **域名和重定向** 部分，然后在“子目录”下点击 **设置子目录**.

输入您要托管文档的 URL。然后指定用于访问文档的子目录，例如 `tomatopy.pizza/docs`，然后点击 **配置**.

在 **其他配置**下方现在会显示一个代理 URL。下一步配置 Cloudflare Worker 时会用到它。请将其复制到剪贴板。
{% endstep %}

{% step %}

#### 创建您的 Cloudflare worker

登录 Cloudflare 账户并前往 **Workers 和 Pages**.

点击 **创建** 按钮。

在“创建应用程序”页面中，点击“从模板开始”卡片上的 **Hello world** 按钮。

为 Worker 起一个更具描述性的名称，例如 `mydocs-subpath-proxy`。重命名 Worker 后，点击 **部署**.
{% endstep %}

{% step %}

### 配置您的自定义域名

Worker 会获得一个可供使用的默认 URL。若要改为配置自定义域名（例如 `tomatopy.pizza`），请点击 **设置。** 然后，在“域名和路由”部分中，点击 **+ 添加**.

在打开的“域名和路由”面板中，点击 **自定义域名**，然后在随后的文本框中输入自定义域名。指定自定义域名时， *不要* 包含子目录。例如， `tomatopy.pizza` 是正确的，而 `tomatopy.pizza/docs` 则不正确。
{% endstep %}

{% step %}

#### 更新 worker 代码

Worker 部署完成后，点击 **编辑代码**，或者点击 **继续前往项目**，然后点击右上角的 **编辑代码** 按钮。

在打开的代码编辑器中，将示例代码替换为以下代码片段：

{% code lineNumbers="true" %}

```javascript
export default {
  fetch(request) { 
    const SUBDIRECTORY = '/docs';
    const url = new URL(request.url);
    const target = "<INSERT YOUR PROXY URL FROM GITBOOK>" + url.pathname.slice(SUBDIRECTORY.length);
    const proxy = new URL(
      target.endsWith('/') ? target.slice(0, -1) : target 
    )
    proxy.search = url.search;
    return fetch(new Request(proxy, request));
  }
};
```

{% endcode %}

{% hint style="info" %}
请务必将第 5 行的 URL 更新为您在第一步中从 GitBook 获得的代理 URL。
{% endhint %}

完成后，点击 **部署**。此过程可能需要一些时间。完成后，访问该 URL 时，你应该能看到文档网站。
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/zh/publish/custom-domain/setting-a-custom-subdirectory/configuring-a-subdirectory-with-cloudflare.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
