一、服务器Nginx反代

反代后的链接为https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
server {
listen 80;
server_name domain_name;
index index.html;
root /www/wwwroot/jsd;

#反盗链
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|gif|jpg|jpeg|png|webp|mp4)$ {
# expires 30d;
# valid_referers none blocked domain_name domain_name_2;
# if ($invalid_referer) {
# return 404;
# }
# }

#阻止用户访问具有特定扩展名的文件
# location ~ \.(zip|rar|7z|apk|ipa|exe|msi|m3u|m3u8|mp4|mp3|avi|gif|jpg|jpeg|png|webp)$ {
# deny all;
# }

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;
proxy_ssl_name cdn.jsdelivr.net;
proxy_ssl_server_name on;

#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2|txt|sh)$" ) {
}
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 4320m;
}
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;
proxy_ssl_name cdn.jsdelivr.net;
proxy_ssl_server_name on;

#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2|txt|sh)$" ) {
}
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 4320m;
}
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;
proxy_ssl_name cdn.jsdelivr.net;
proxy_ssl_server_name on;

#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2|txt|sh)$" ) {
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 4320m;
}

# set site favicon
location = /favicon.ico {
log_not_found off;
access_log off;
}

#清理缓存规则
location ~ /purge(/.*) {
proxy_cache_purge cache_one $host$1$is_args$args;
#access_log /www/wwwlogs/js_purge_cache.log;
}

access_log /www/wwwlogs/jsd.log;
error_log /www/wwwlogs/jsd.error.log;
}

二、PHP 服务器反代

该方法适用于有PHP环境虚拟机,可同时反代cdn.jsdelivr.net​、raw.githubusercontent.com​和github.com​ 的 Releases 和 Archive 。

使用方法:将下面的代码保存为jsd.php​,得到链接格式为https://example.com/jsd.php?url=https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
$allowed_domains = array(
'cdn.jsdelivr.net',
'raw.githubusercontent.com',
'github.com'
);

if (!isset($_GET['url'])) {
die("请将参数填写完整,在当前路径后加上?url=反代的链接");
}

$url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST); // 获取 URL 中的域名

if (in_array($host, $allowed_domains)) { // 验证域名是否符合要求
// 设置响应头部 Content-Type 和 Content-Disposition
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: inline; filename="' . basename($url) . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if (isset($_SERVER['HTTP_REFERER'])) {
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
}

$data_down = curl_exec($ch);
if ($data_down === false) {
die("代理时发生错误");
}

curl_close($ch);

// 检测文件编码是否为 UTF-8
if (!mb_check_encoding($data_down, 'UTF-8')) {
//文件编码不是 UTF-8,直接下载
header('Content-Type:' . curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
header('Content-Disposition: attachment; filename="' . basename($url) . '"');
echo $data_down;
exit();
}

echo $data_down;
} else { // 域名不符合要求
die('本站不支持该链接');
}
?>

可配合前端主页使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>加速访问</title>
<style>
body {
background-color: #f0f0f0;
font-family: 'Arial', sans-serif;
}

div {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 10%;
}

h1 {
text-align: center;
margin-bottom: 5%;
color: #292b30;
}

input[type="text"] {
width: 50%;
height: 40px;
font-size: 16px;
text-align: center;
border-radius: 20px;
border: none;
background-color: #ebebeb;
margin-bottom: 3%;
box-shadow: 0 0 5px #a9a9a9;
}

button {
width: 20%;
height: 40px;
font-size: 16px;
color: #fff;
background-color: #292b30;
border-radius: 20px;
border: none;
cursor: pointer;
box-shadow: 0 0 5px #292b30;
transition: all 0.2s ease-in-out;
}

button:hover {
background-color: #fff;
color: #292b30;
box-shadow: 0 0 10px #292b30;
}

@media only screen and (max-width: 600px) {
input[type="text"] {
width: 80%;
}

button {
height: 30px;
}
}
</style>
<script>
function goToUrl() {
var url = document.getElementById("input").value;
var domains = ["cdn.jsdelivr.net", "raw.githubusercontent.com", "github.com"];
var urlDomain = new URL(url).hostname;

if (domains.includes(urlDomain)) {
window.location.href = "/jsd.php?url=" + decodeURIComponent(url);
} else {
var tip = document.createElement("div");
tip.innerText = "不支持该域名";
tip.style.cssText = "color:red;font-size:12px;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);transition:opacity 0.5s ease-in-out;opacity:1;z-index:10000;";
document.body.appendChild(tip);

setTimeout(function () {
document.body.removeChild(tip);
}, 3000);
}
}

// 添加事件监听器
window.onload = function () {
document.getElementById("input")
.addEventListener("keyup", function (event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("goBtn").click();
}
});
};
</script>
</head>

<body>
<h1>加速访问服务</h1>
<div>
<input type="text" id="input" name="input" placeholder="请输入链接(带HTTP/HTTPS):" required><br>

<button id="goBtn" onclick="goToUrl()">前往</button>
</div>
</body>

</html>