两种方式反代jsdelivr

前言

jsDelivr是一个免费、快速、可靠的为JS和开源项目服务的CDN,但是在国内访问的速度有点感人。

开源地址

Github:https://github.com/54ayao/JSDMirror

教程

cloudflare workers

1.将如下代码添加到cloudflare workers中的workers.js中

# 整体架构是这样的,这里面大部分都是正则,可以利用正则匹配来引用到其他cdn里面 这种写法比较简单,而且还能直接使用在cf里面,其他的你需要自行转换,正则匹配的路径

self.addEventListener('fetch', event => {
    const url = new URL(event.request.url);
    let newHostnameOrIp = '';

    const pathToHostMap = [
        // 具体的 npm 包路径匹配(包含可选的版本号) 黑名单npm
        {
            regex: /^/npm/chenyfan-happypic-sex(@[A-Za-z0-9.-]+|/)?/,
            hostname: '418.php'
        },

        // GitHub 仓库或特定前缀的路径匹配 黑名单仓库

        {
            regex: /^/gh/(.*.)?(wodafei|qucomic|90666225|nanjingup|fznb1234|niunai88|wenmou258|cannian666999|wode-1u|woshishei138|niuzai12345|03298|oka159|nansheng-521zx|jackson0829|suxing12312|bodaxsd|ljxi|qxqx11|mumuwocal|mxqotz|miaolou|muimg1|yixuan66|qdqqd|Airmole|qwehggfgj|yixuan66|wdzhwsh4067|spfans996|sjn000|qihang119|pengzhangsir|xiaohaiya|xz211|ueletv|shijianzhong|bmw88888888|sickikco)(/.*)?($|?)/,
            hostname: '418.php'
        },
        // 匹配 npm、gh、china 下的图片文件 提交腾讯云审核渠道
        {
            regex: /^/(npm|gh|china)/(.*.)?(jpg|jpeg|gif|ico|png|bmp|webp|psd|tif|tiff|svg|avif|exif|heif|heic|dng|cr2|nef|orf|raf|rw2|pem|pgm|ppm|xbm|xpm|cur|anim|flc|fli|flx|fpx|wmf|emf|ai|eps|drw|dxf|pct|pctx|pgm|ppm|psd|psb|xcf|xwd|yuv|fits)($|?|/)/,
            hostname: '审核服务器'
        },

        // 匹配 npm、gh、wp、china 下的多级目录路径 修改jsdelivr为jsdmirror
        {
            regex: /^/(npm|gh|wp|china|combine)/([^/]+/)+$/,
            hostname: 'cdn.jsdmirror.com.dir.nginx.conf'
        },

        // 匹配 npm、combine、cdn-cgi、gh、wp、china 开头的任意路径(作为默认或捕获剩余情况)
        {
            regex: /^/(npm|combine|cdn-cgi|gh|wp|china)//,
            hostname: 'fastly.jsdelivr.net'
        },

        // 修改首页
        {
            regex: /^/*/,
            hostname: 'index.html'
        },


        // 如果确实需要一个“捕获所有”的规则,请确保它是最后一个,并且前面的规则足够具体以避免不必要的覆盖
    ];

    // 遍历映射,找到匹配的路径
    for (const {
            regex,
            hostname
        }
        of pathToHostMap) {
        if (regex.test(url.pathname)) {
            newHostnameOrIp = hostname;
            break;
        }
    }

    // 如果没有找到匹配的路径,则使用原始的主机名(可选)
    if (!newHostnameOrIp) {
        event.respondWith(fetch(event.request));
        return;
    }

    // 修改请求的URL以使用新的主机名或IP
    url.hostname = newHostnameOrIp;

    // 创建新的请求对象(注意:这里不需要传递event.request作为第二个参数,因为Request构造函数不需要它)
    const newRequest = new Request(url.toString());

    // 响应新的请求
    event.respondWith(fetch(newRequest));
});

Nginx

将如下代码添加到需要反代域名的Nginx配置文件中

data.jsdelivr.com


#PROXY-START/

location ^~ /
{
    proxy_pass https://data.jsdelivr.com;
    proxy_set_header Host data.jsdelivr.com;
    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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
     
    proxy_ssl_server_name on; 
    # proxy_hide_header Upgrade;
    proxy_set_header Accept-Encoding "";
     # 替换 <title> 标签中的文本  
    sub_filter '<title>jsDelivr' '<title>JSDMirror';  
    sub_filter '<title>JsDelivr' '<title>JSDMirror'; # 处理大写情况  
    sub_filter '<title>JSDELIVR' '<title>JSDMirror'; # 处理全大写情况  
  
    # 替换 <text> 标签中的文本(确保 x, y, transform 等属性不会变化)  
    sub_filter '<text .*?>jsDelivr</text>' '<text $1>JSDMirror</text>'; # 注意这里使用 $1 是不准确的,因为 sub_filter 不支持捕获组  
  
 sub_filter '<text x="265" y="140" transform="scale(.1)" fill="#fff" textLength="410">jsDelivr</text>' '<text x="265" y="140" transform="scale(.1)" fill="#fff" textLength="410">JSDMirror</text>';  
    sub_filter_once off;  
    sub_filter_types application/xml image/svg+xml; # 确保对 SVG 应用替换

    add_header X-Cache $upstream_cache_status;
		#Set Nginx Cache


    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 120m;
}

#PROXY-END/

fastly.jsdelivr.net


#PROXY-START/

location ~* ^/(npm|gh|wp|china|combine)/([^/]+/?)+
{
    proxy_pass https://fastly.jsdelivr.net;
    proxy_set_header Host fastly.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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
            proxy_cache_valid 200 304 301 302 30d;
            	expires 30d;
    # proxy_hide_header Upgrade;
 
    proxy_ssl_server_name on;
    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    proxy_set_header Accept-Encoding "";
		sub_filter "Learn more about jsDelivr" " Learn more about JSDMirror";
				sub_filter "	<svg class="logo" viewBox="0 0 140 34" xmlns="https://www.w3.org/2000/svg">n					<g fill="#1e3b45">n						<pathn							d="m43.616 19.576c0 4.005-1.389 6.008-4.168 6.008-.432 0-.853-.059-1.261-.174v-1.112c.455.154.883.232 1.285.232 1.065 0 1.833-.404 2.304-1.209.471-.807.706-2.063.706-3.769v-10.859h1.134z"/>n						<pathn							d="m47.065 24.705v-1.309c1.188.756 2.393 1.135 3.612 1.135 1.296 0 2.28-.268 2.952-.805.671-.537 1.007-1.288 1.007-2.251 0-.85-.226-1.528-.678-2.032-.451-.506-1.429-1.194-2.935-2.067-1.682-.979-2.747-1.799-3.195-2.46-.447-.66-.671-1.422-.671-2.286 0-1.172.455-2.169 1.366-2.988.911-.817 2.126-1.227 3.647-1.227.988 0 1.975.166 2.963.498v1.204c-.972-.44-2.011-.66-3.113-.66-1.127 0-2.02.287-2.681.858-.659.571-.99 1.297-.99 2.177 0 .848.226 1.524.677 2.026.452.5 1.426 1.184 2.923 2.048 1.551.881 2.584 1.663 3.097 2.345.514.684.77 1.469.77 2.356 0 1.274-.442 2.311-1.325 3.114-.884.803-2.133 1.204-3.746 1.204-.572 0-1.229-.09-1.973-.266-.745-.178-1.313-.383-1.707-.614z"/>n						<pathn							d="m59.104 25.295v-16.602h5.881c5.895 0 8.844 2.698 8.844 8.093 0 2.585-.805 4.65-2.413 6.194-1.61 1.543-3.753 2.315-6.431 2.315zm3.74-13.556v10.522h1.852c1.621 0 2.892-.485 3.814-1.458s1.383-2.296 1.383-3.97c0-1.583-.457-2.827-1.372-3.734-.914-.907-2.199-1.36-3.85-1.36z"/>n						<pathn							d="m86.841 25.295h-9.957v-16.602h9.574v3.046h-5.834v3.693h5.43v3.032h-5.43v3.796h6.217z"/>n						<path d="m99.893 25.295h-9.887v-16.602h3.74v13.568h6.147z"/>n						<path d="m105.989 8.693v16.602h-3.74v-16.602z"/>n						<pathn							d="m123.834 8.693-5.719 16.602h-4.236l-5.651-16.602h4.029l3.462 11.553c.186.625.297 1.178.336 1.657h.068c.055-.518.174-1.084.36-1.702l3.439-11.508z"/>n						<pathn							d="m140 25.295h-4.295l-2.581-4.273c-.193-.322-.379-.613-.555-.868-.178-.254-.358-.473-.539-.654-.182-.18-.369-.321-.567-.416-.197-.096-.41-.145-.643-.145h-1.006v6.356h-3.74v-16.602h5.926c4.029 0 6.043 1.506 6.043 4.515 0 .578-.088 1.114-.266 1.604s-.428.932-.752 1.325c-.324.395-.717.733-1.176 1.02-.459.285-.969.508-1.534.67v.047c.248.076.486.203.719.375.231.174.455.377.67.61.217.231.424.479.619.746.197.266.377.526.539.782zm-10.185-13.8v4.619h1.62c.803 0 1.448-.231 1.932-.694.494-.471.742-1.053.742-1.749 0-1.45-.87-2.177-2.605-2.177h-1.689z"/>n					</g>n					<path d="m15.386.338-3.106 11.038v.104 11.039l3.106 11.143 3.194-11.143v-11.039-.104z"n						  fill="#bd483b"/>n					<path d="m15.386.338-15.386 5.542 2.186 20.492 13.2 7.29" fill="#e64e3d"/>n					<path d="m15.386 33.662 13.268-7.365 2.483-20.49-15.751-5.469" fill="#bd483b"/>n					<pathn						d="m12.594 25.088c-1.514-.473-2.864-1.317-3.94-2.431l-.003-.002c-.131-.137-.257-.274-.381-.418-.838-.979-1.478-2.13-1.857-3.396.251.233.518.447.796.647.003.008.008.016.011.027-.003-.012-.008-.02-.011-.027.398.279.822.526 1.269.737.141.064.282.125.427.186.177.07.36.135.542.195.011.006.024.006.035.01.032.012.065.023.097.033.074.756.649 1.372 1.39 1.504.287 1.157.833 2.146 1.625 2.935z"n						fill="#fec82f"/>n					<pathn						d="m13.174 11.794c0 .324.088.627.243.883-1.25 1.753-2.108 3.656-2.479 5.539-.041.209-.077.416-.105.619-.429.113-.79.393-1.016.762-.013 0-.024-.004-.035-.01-.023-.006-.04-.014-.061-.021-.142-.045-.281-.098-.417-.152-.204-.08-.403-.174-.598-.272-.663-.338-1.26-.772-1.781-1.291-.11-.111-.213-.219-.311-.332l-.041-.049c-.038-.045-.078-.092-.115-.137-.017-.021-.032-.039-.047-.059-.014-.018-.024-.031-.037-.045-.005-.01-.013-.016-.017-.023-.02-.022-.037-.047-.053-.068-.008-.012-.017-.022-.023-.029-.001-.004-.002-.004-.004-.008-.013-.014-.024-.033-.037-.049-.055-.072-.107-.149-.157-.225-.009-.012-.019-.024-.025-.039-.006-.006-.015-.018-.02-.027-.014-.203-.02-.408-.02-.617 0-1.882.557-3.636 1.512-5.105.113-.176.235-.348.361-.514.12-.16.245-.319.374-.467 1.126-1.317 2.61-2.315 4.299-2.847.026.182.059.367.095.553.192.967.513 1.942.949 2.898-.271.3-.434.698-.434 1.132z"n						fill="#fec82f"/>" "<img src="https://img.cuteapi.com/2023/08/29/Background.png" width="185" height="60" alt="Background Image">";
				sub_filter "<pathn						d="m12.176 20.479c0 .221-.079.424-.212.58-.029.037-.061.068-.096.1-.161.141-.368.225-.596.225-.173 0-.335-.049-.472-.135-.147-.09-.265-.219-.342-.375-.058-.121-.089-.252-.089-.395 0-.26.11-.494.286-.658.029-.027.06-.051.091-.074.148-.107.331-.17.526-.17.206 0 .395.068.546.186.085.063.155.139.213.229.094.137.145.307.145.487z"n						fill="#fec82f"/>n					<pathn						d="m15.777 11.794c0 .147-.032.281-.094.403-.148.299-.456.502-.808.502-.044 0-.087-.002-.128-.006-.008-.004-.016-.004-.025-.006-.383-.066-.684-.369-.741-.756-.007-.043-.01-.09-.01-.137 0-.102.017-.201.05-.295.123-.354.46-.606.854-.606h.002.036c.392.018.72.285.827.645.025.082.037.168.037.256z"n						fill="#fec82f"/>n					<pathn						d="m24.752 16.143c0 .907-.129 1.782-.368 2.61-.799-.211-1.606-.52-2.4-.914.022-.109.033-.221.033-.336 0-.225-.044-.442-.125-.639.031-.029.064-.061.095-.094.957-.977 1.763-2.055 2.404-3.212.234.821.361 1.69.361 2.585z"n						fill="#df9c26"/>n					<pathn						d="m23.881 12.196c-.063.139-.131.277-.201.416-.627 1.235-1.455 2.382-2.459 3.407-.009.01-.02.02-.028.027-.255-.156-.557-.244-.879-.244-.375 0-.722.123-1.004.328-.514-.404-1.011-.848-1.49-1.327-.608-.604-1.157-1.247-1.647-1.909.252-.297.405-.68.405-1.102 0-.313-.087-.61-.237-.862 1.21-1.163 2.547-2.106 3.917-2.788 1.572.961 2.841 2.372 3.623 4.054z"n						fill="#df9c26"/>n					<pathn						d="m21.217 17.503c0 .379-.23.701-.556.836-.108.045-.225.07-.348.07-.063 0-.125-.008-.185-.02-.385-.082-.681-.408-.715-.805.011-.01.021-.016.028-.022-.01-.008-.021-.014-.03-.023-.001-.012-.001-.022-.001-.037 0-.389.25-.723.601-.85.095-.033.196-.053.302-.053.09 0 .179.014.262.039.346.105.606.412.64.785.002.027.002.055.002.08z"n						fill="#df9c26"/>n					<pathn						d="m21.452 18.767c-.301.274-.7.44-1.139.44-.351 0-.677-.107-.949-.289-.039.025-.078.051-.115.072-1.233.781-2.538 1.352-3.864 1.698v4.824c3.887 0 7.222-2.37 8.64-5.744-.859-.237-1.723-.573-2.573-1.001z"n						fill="#df9c26"/>n					<pathn						d="m15.386 20.688c-.793.205-1.591.33-2.385.367-.042.002-.086.006-.128.008-.151.41-.454.744-.839.94.245.909.688 1.698 1.319 2.327.524.524 1.162.92 1.891 1.18.046 0 .093.002.142.002z"n						fill="#fec82f"/>n					<pathn						d="m18.612 17.503c0-.172.026-.34.074-.498-.562-.44-1.106-.92-1.625-1.442-.614-.614-1.172-1.262-1.675-1.934v5.946c1.124-.324 2.235-.823 3.291-1.489.009-.006.02-.014.03-.022-.061-.174-.095-.364-.095-.561z"n						fill="#df9c26"/>n					<pathn						d="m15.386 13.629c-.045-.059-.091-.113-.132-.174-.123.029-.249.043-.378.043-.227 0-.441-.045-.637-.123-1.134 1.606-1.912 3.341-2.25 5.049-.032.162-.059.32-.083.475.477.195.848.596.996 1.092.016-.004.029-.004.046-.004.809-.039 1.627-.18 2.438-.412z"n						fill="#fec82f"/>n					<pathn						d="m15.386 6.778v3.394c.048.016.098.033.145.055 1.106-1.073 2.316-1.979 3.573-2.681-1.14-.496-2.399-.768-3.718-.768z"n						fill="#df9c26"/>n					<pathn						d="m15.386 6.778c-.608 0-1.201.055-1.773.168.025.197.06.404.101.606.168.86.449 1.725.829 2.575.106-.02.219-.033.333-.033.178 0 .347.027.51.078z"n						fill="#fec82f"/>n				</svg>" "";
					sub_filter "jsdelivr.com, 2012 - 2024" "jsdmirror.com <br> 2021- 2024";
sub_filter '<a href="https://github.com/jsdelivr/jsdelivr">' '<a href="https://beian.miit.gov.cn">辽ICP备2023005487号-9<a href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=21028102000208" >  
        辽公网安备21028102000208号
    <a href="https://github.com/54ayao/jsdmirror">
';
		sub_filter "jsDelivr" "JSDmirror";
	sub_filter "data.jsdelivr.com" "data.jsdmirror.com";	
	sub_filter "www.jsdelivr.com" "www.jsdmirror.com";	
		sub_filter "cdn.jsdelivr.net" "cdn.jsdmirror.com";	
    sub_filter_once off;
    


}
       
  
  
#PROXY-END/

结语

因为cloudflare workers有免费版的次数限制,所以最好自己使用

 

提示:本文最后更新于2024年 8月 26日,如有错误或者已经失效,请留言告知。
THE END