netlify反代

起因

我在使用 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: The path you want to redirect.

to: The URL or path you want to redirect to.

status: The HTTP status code you want to use in that redirect; 301 by default.

force: Whether to override any existing content in the path or not; false by default. Visit the shadowing instructions for more details.

query: Query string parameters REQUIRED to match the redirect. Visit the query parameters instructions for more details.

conditions: Conditions to match the redirect, including country, role, and cookie presence conditions.

headers: Additional request headers to send in proxy redirects.

signed: Name of an environment variable for signed proxy redirects.

目录反代目录匹配

因为反代 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的文件中

注意这里的文件必须在仓库的根目录

Copy
[[redirects]]
from = "/*"
to = "https://cdn.jsdelivr.net/:splat"
status = 200
force = true
headers = {X-From = "Netlify"}

然后把这个仓库上传到 github

之后我们点击这里在 netlify 新建一个项目并且选择已经上传到 github 仓库

然后等待部署完成即可

完结撒花👏