起因#
我在使用 netlify 部署博客后,经过测试速度,我觉得作为一款非国内产品速度相当不错
所以我想到了我部署在 vercel 的 jsd 反代 如果我把反代部署在 netlify 那么 cdn 的速度会有提升 说干就干
ps: 如果你不想看我叨叨,你可以点击跳转到代码处直接查看代码使用
需要的东西#
一個 netlify 帳號
一個 github 帳號
查閱文檔#
打開 netlify 的文檔查看
格式#
找到了路由設置的相關內容後可以看到這些內容和格式
If you specify your redirect rules in your Netlify configuration file, you can use a more structured configuration format with additional capabilities such as signed proxy redirects. In a netlify.toml file, we use TOML’s array of tables to specify each individual redirect rule. The following keywords are available:
from
: 你想要重定向的路徑。
to
: 你想要重定向到的 URL 或路徑。
status
: 你想在該重定向中使用的 HTTP 狀態碼;默認為 301。
force
: 是否覆蓋路徑中的任何現有內容;默認為 false。訪問 shadowing 指令以獲取更多詳細信息。
query
: 匹配重定向所需的查詢字符串參數。訪問查詢參數指令以獲取更多詳細信息。
conditions
: 匹配重定向的條件,包括國家、角色和 cookie 存在條件。
headers
: 在代理重定向中發送的附加請求標頭。
signed
: 用於簽名代理重定向的環境變量名稱。
目錄反代目錄匹配#
因為反代 jsd 同時需要反代 jsd 目錄下的所有文件,所以這裡需要有一個路徑匹配
原文如下
An asterisk indicates a splat that will match anything that follows it.
You can use the splat in your rewrites or redirects like this:
/news/* /blog/
This would redirect paths like /news/2004/01/10/my-story to /blog/2004/01/10/my-story.
所以我需要在 from
的 / 後面添加 * to
的 / 後面添加
實踐代碼#
這裡我需要 http 返回狀態碼為 200
這樣我的國內伺服器就能再次反代 netlify 的 cdn
整合完上面的設置配置就是這樣了
這裡我們需要把下面的代碼放在新建名為 netlify.toml
的文件中
注意這裡的文件必須在倉庫的根目錄
[[redirects]]
from = "/*"
to = "https://cdn.jsdelivr.net/:splat"
status = 200
force = true
headers = {X-From = "Netlify"}
然後把這個倉庫上傳到 github
之後我們點擊這裡在 netlify 新建一個項目並且選擇已經上傳到 github 倉庫
然後等待部署完成即可
完結撒花👏