---
abbrlink: '23787'
author: qystudio
bottom_meta: true
categories:
- - web
comments: true
cover: false
date: '2022-12-29 21:18:42'
description: netlify配置反代
headimg: https://cdn.chuqis.com/img/4c8a395ea13de4893afb0921e74afeef.jpg
indent: true
keywords: netlify反代
mathjax: false
pin: false
references:
- title: netlify文档
  url: https://docs.netlify.com/routing/redirects/
tags:
- netlify
- 反代
- jsdelivr
title: netlify反代
toc: true
top_meta: true
updated: '2022-12-30 16:38:37'
---
## 起因

我在使用netlify部署博客后，经过测试速度，我觉得作为一款非国内产品速度相当不错

![https://cdn.chuqis.com/img/35b2aaa74553ddcf3b4e9765d2a3df0b.jpg](https://cdn.chuqis.com/img/35b2aaa74553ddcf3b4e9765d2a3df0b.jpg)

所以我想到了我部署在vercel的jsd反代
如果我把反代部署在netlify那么cdn的速度会有提升
说干就干

ps:如果你不想看我叨叨，你可以点击跳转到[代码处](/posts/23787.html#实践代码)直接查看代码使用

## 需要的东西

一个netlify账号

![https://cdn.chuqis.com/img/4c8a395ea13de4893afb0921e74afeef.jpg](https://cdn.chuqis.com/img/4c8a395ea13de4893afb0921e74afeef.jpg)

一个github账号

## 查阅文档

打开netlify的[文档](https://docs.netlify.com/routing/redirects/)查看

### 格式

找到了路由设置的相关内容后可以看到这些内容和格式

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/:splat

This would redirect paths like /news/2004/01/10/my-story to /blog/2004/01/10/my-story.

所以我需要在 `from`的/后面添加* `to`的/后面添加:splat

## 实践代码

这里我需要http返回状态码为200

> 这样我的国内服务器就能再次反代netlify的cdn

整合完上面的设置配置就是这样了

这里我们需要把下面的代码放在新建名为 `netlify.toml`的文件中

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

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

然后把这个仓库上传到github

之后我们点击[这里](https://app.netlify.com/start)在netlify新建一个项目并且选择已经上传到github仓库

然后等待部署完成即可

完结撒花👏
