自建jsdelivr镜像加速服务
以宝塔为例,创建一个网站。
找到反向代理,开启缓存和高级功能,分别配置npm、gh以及wp的镜像,并配置缓存。
缓存时间填写43200分钟,当然这个可以按照自己的喜好来配置,时间长的话性能会比较好。
创建三个反向代理,代理目录分别为:
目标url分别为:/npm/ # npm路径
/gh/ # github路径
/wp/ # wordpress路径
发送域名都为cdn.jsdelivr.nethttps://cdn.jsdelivr.net/npm/ # npm路径
https://cdn.jsdelivr.net/gh/ # github路径
https://cdn.jsdelivr.net/wp/ # wordpress路径
当然也不必每个都配置,假如你只需要使用github的镜像,那么就只配置gh即可。
分开配置代理目录的好处就是域名访问的首页可以自定义,如果直接全站反代的话会直接跳转到jsdelivr官网。
Nginx配置文件:
location ^~ /npm/
{
proxy_pass https://cdn.jsdelivr.net/npm/;
proxy_set_header Host cdn.jsdelivr.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 43200m;
}
location ^~ /gh/
{
proxy_pass https://cdn.jsdelivr.net/gh/;
proxy_set_header Host cdn.jsdelivr.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 43200m;
}
location ^~ /wp/
{
proxy_pass https://cdn.jsdelivr.net/wp/;
proxy_set_header Host cdn.jsdelivr.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
expires 12h;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 43200m;
}
刷新缓存
在URL中添加/purge/即可刷新,例如:
缓存链接:https://gh.bingdou.com.cn/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png
执行刷新:https://gh.bingdou.com.cn/purge/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png
配置CDN加速
按照正常的CDN加速配置即可,也可配置缓存,建议加上防盗链,以防止滥用,当然公共服务除外。
使用
将项目引用链接中的cdn.jsdelivr.net替换为你绑定的域名即可,这里我绑定的域名是gh.bingdou.com.cn
效果如下:
https://gh.bingdou.com.cn/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png
https://gh.bingdou.com.cn/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png