此前,jsdelivr项目的中国域名cdn.jsdelivr.net备案被注销,国内的线路质量非常的不稳定,于是很多采用jsdelivr作为静态文件加速服务的都遭受的了不同程度的影响,现在的加载速度和稳定性很难与之前有备案的情况下相比,当然,此状况出现的原因我个人认为是遭到大幅度滥用导致的,如果你的项目在使用jsdelivr,我们可以通过自建镜像的方式对jsdelivr进行加速,当然为了安全,还是推荐配置防盗链,仅供自己使用。

宝塔配置反向代理

以宝塔为例,创建一个网站

找到反向代理,开启缓存和高级功能,分别配置npm、gh以及wp的镜像,并配置缓存

缓存时间填写43200分钟,当然这个可以按照自己的喜好来配置,时间长的话性能会比较好。

创建三个反向代理,代理目录分别为

/npm/  # npm路径
/gh/   # github路径
/wp/   # wordpress路径

目标url分别为:

https://fastly.jsdelivr.net/npm/ # npm路径
https://fastly.jsdelivr.net/gh/  # github路径
https://fastly.jsdelivr.net/wp/  # wordpress路径

发送域名都为cdn.jsdelivr.net
当然也不必每个都配置,假如你只需要使用github的镜像,那么就只配置gh即可。
分开配置代理目录的好处就是域名访问的首页可以自定义,如果直接全站反代的话会直接跳转到jsdelivr官网。

Nginx配置文件

location ^~ /npm/
{
    proxy_pass https://fastly.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://fastly.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://fastly.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/即可刷新,例如:

使用

将项目引用链接中的cdn.jsdelivr.net替换为你绑定的域名即可,这里我绑定的域名是jsdelivr.170601.xyz
效果如下:
https://fastly.jsdelivr.net/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png
https://jsdelivr.170601.xyz/gh/kaygb/twentytwenty/vwmwv/2022/202112161541063.png

如果觉得我的文章对你有用,请随意赞赏