记一次Github镜像站搭建

记一次Github镜像站搭建

起因

迫于github在国内日常打不开,以及我刚好有一台吃灰的香港vps,所以我决定自己建一个Github的镜像站。

虽然我日常开全局,几乎无缝访问Github,但是有时候需要在别人电脑上使用Github,所以建一个镜像站还是有必要的。

所以镜像站点搭建的前置条件是:

  • 一个能访问原站点的vps
  • 一个域名

请求分析

建镜像站之前,我们首先要分析一下github的请求,这种大型网站的资源肯定是单独存放的,所以我们需要镜像的域名肯定不止一个。

打开开发者工具,访问github首页,在网络选项卡中可以看到加载的所有请求,我们可以一个一个查看域名,然后把不同的域名记下来就好了

Github请求资源

当然,还有更快捷的方法,直接切换到源代码选项卡,忽略浏览器扩展加载的文件,其他的便是我们需要镜像的域名

Github请求资源

如图,一共请求了三个域名的资源

  • github.com
  • github.githubassets.com
  • avatars.githubusercontent.com

当然,这还只是首页请求的资源,其实另外还有几个资源域名,如githun的查看源文件的域名raw.githubusercontent.com

但是,我们不一定需要镜像所有域名,因为有些域名并没有被墙 [doge]

我这里直接说结论,github被墙的域名只有下面两个罢

  • github.com
  • raw.githubusercontent.com

镜像过程

添加域名解析

域名添加解析我就不多说了,我这边使用cloudflare管理域名,直接在cf添加A记录解析ip到vps即可,需要镜像几个域名就添加几条记录,当然你也可以添加泛解析

域名记录

nginx反代

说是镜像,其实就是利用了nginx的反代功能,

我这边图个方便直接装了宝塔面板

在宝塔面板里添加站点,域名填刚刚解析的域名,因为需要反代不同的站点,所以每个域名需要单独建一个站点。(github.comraw.githubusercontent.com)

Snipaste_2022-04-23_10-42-40

添加完成之后点开设置,在反向代理选项卡中添加反向代理,将每个站点分别反代到不同的域名。

如图为反代到github域名的设置,注意目标url那里需要用https://,另外还需要添加一个内容替换,把github.com替换为自己反代的域名,把github的资源请求也指向镜像站

添加反代

添加反向代理

添加完成之后打开我们的镜像站,如果顺利的话,恭喜你,一个可以clone镜像站已经搭建完成了

镜像网站

随便打开一个项目,你会发现替换生效了,clone地址变成了你镜像的地址,而且这个地址是可用的,我就不演示了

项目

clone镜像是搞定了,但是网页还是存在一些问题,随便点开项目中的一个文件,你会发现加载不出来,并且控制台报错了

控制台报错

这是由于github采用了pjax,然后pjax的地址并没有被替换成镜像地址导致的,我们可以通过nginx的配置来解决

回到宝塔面板,打开反向代理配置文件(站点设置-反向代理-配置文件)

将下面两行加入配置文件

1
2
3
   #解决pjax问题
proxy_hide_header x-pjax-url;
add_header x-pjax-url "https://7mm.top$request_uri";

反代配置

现在pjax就没有问题了,可以随意打开项目文件了,至此镜像站已经完成了

等等,好像忘了什么,点开一个项目文件,再点击raw,你会发现访问不了

最后一步,我们来搭建raw的镜像

还是建立站点、添加反代

Snipaste_2022-04-23_10-42-40

现在访问raw镜像站会跳转到github首页,

我们随便找个raw地址,将url中的raw.githubusercontent.com替换为镜像地址

raw镜像

现在将我们github镜像站的raw只想我们的raw镜像站

修改github镜像站的反代设置,添加一个内容替换

//raw.githubassets.com替换为我们的raw镜像站

Snipaste_2022-04-23_10-42-40

再打开反代配置文件,添加一行

1
add_header x-pjax-url "https://7mm.top$request_uri";

修改配置文件

至此,镜像站点的搭建已经完成,enjoy!

总结

github.com镜像nginx配置文件

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
server {
listen 80;
listen 443 ssl http2;
server_name xxx.top;
index index.php index.html index.htm default.php default.htm default.html;


#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
ssl_certificate /www/server/panel/vhost/cert/7mm.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/7mm.top/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;


location ~ /purge(/.*) {
proxy_cache_purge cache_one $host$1$is_args$args;
#access_log /www/wwwlogs/7mm.top_purge_cache.log;
}
#反代配置
location ^~ / {
proxy_pass https://github.com;
proxy_set_header Host github.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;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache

#解决pjax问题
proxy_hide_header x-pjax-url;
add_header x-pjax-url "https://7mm.top$request_uri";

#raw替换
proxy_redirect https://raw.githubusercontent.com https://raw.7mm.top;

proxy_set_header Accept-Encoding "";

sub_filter_once off;

sub_filter "//github.com" "//7mm.top";
sub_filter "//raw.githubassets.com" "//raw.7mm.top";

set $static_filexskBQvHR 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) {
set $static_filexskBQvHR 1;
expires 12h;
}
if ( $static_filexskBQvHR = 0 ) {
add_header Cache-Control no-cache;
}
}


access_log /www/wwwlogs/7mm.top.log;
error_log /www/wwwlogs/7mm.top.error.log;
}

raw.githubusercontent镜像nginx配置

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
server {
listen 80;
listen 443 ssl http2;
server_name raw.xxx.top;

#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
ssl_certificate /www/server/panel/vhost/cert/7mm.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/7mm.top/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;


location ~ /purge(/.*) {
proxy_cache_purge cache_one $host$1$is_args$args;
}
#PROXY-START/

#raw反代配置
location ^~ / {
proxy_pass https://raw.githubusercontent.com;
proxy_set_header Host raw.githubusercontent.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;

add_header X-Cache $upstream_cache_status;

#proxy_hide_header content-security-policy;
#proxy_hide_header Strict-Transport-Security;
#proxy_hide_header set-cookie;
#proxy_hide_header x-pjax-url;

#Set Nginx Cache
set $static_fileHDokVEBC 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) {
set $static_fileHDokVEBC 1;
expires 12h;
}
if ( $static_fileHDokVEBC = 0 ) {
add_header Cache-Control no-cache;
}
}

#PROXY-END/

access_log /www/wwwlogs/7mm.top.log;
error_log /www/wwwlogs/7mm.top.error.log;
}

记一次Github镜像站搭建
http://blog.233c.cn/posts/10013/
作者
木末君
发布于
2022年4月23日
许可协议