飘在云端

东西南北,海角天涯

· 随笔 · · 1688次浏览

GitHub 反向代理 镜像站 支持登录账号,加速访问及下载

更新:2023-2-15 我看到不少人私信我如何部署镜像站,文章我已经给了上游项目的 GitHub 地址了,到他们的子仓库(地址在文末鸣谢的第一行),可以看到里面是一些 nginx 反向代理的配置文件,依葫芦画瓢即可,下面是一个示例: github.com 主站

server
{
  #listen 80 reuseport;
    listen 443 ssl http2 reuseport;
    #listen 443 ssl http2;
    server_name hub.0z.gs;
    #root /web/nginx/stop;
    
    client_max_body_size 0;
        gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_vary on; 
    gzip_proxied any; # test required
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/json
        application/xml
        application/rss+xml
        image/svg+xml;
    
    if ($server_port !~ 443){
       rewrite ^(/.*)$ https://$host$1 permanent;
    }
    
    add_header Permissions-Policy interest-cohort=();
    
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /web/nginx/vhost/cert/github.0z.gs/fullchain.pem;
    ssl_certificate    /web/nginx/vhost/cert/github.0z.gs/fullchain.pem;
    ssl_certificate_key    /web/nginx/vhost/cert/github.0z.gs/privkey.pem;
    ssl_protocols 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 60m;
    ssl_session_tickets off;
    
    #add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;
 
     # releases download
    location ~ ^/[^/]+/[^/]+/releases/download/ {
        return 301 https://download.0z.gs$request_uri;
    }

    # archive download
    location ~ ^/[^/]+/[^/]+/archive/ {
        return 301 https://archive.0z.gs$request_uri;
    }
    
    location ~ ^/[^/]+/[^/]+/suites/[^/]+/artifacts/ {
        return 301 https://download.0z.gs$request_uri;
    }
    
    # Fuck Search Engine
    location /robots.txt {
        allow all;
    }
    
    location / {
    
    proxy_set_header Access-Control-Allow-Origin *;
    proxy_set_header Access-Control-Allow-Methods 'GET, POST, OPTIONS DELETE HEAD';
    proxy_set_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Set-Cookie,Cookie,x-pjax-url';
    add_header 'Access-Control-Allow-Credentials' 'true';

    if ($request_method = 'OPTIONS') {
        return 204;
    }
    
        proxy_hide_header referrer-policy;
        proxy_hide_header content-security-policy;
        proxy_hide_header Strict-Transport-Security;
        #proxy_hide_header set-cookie;
        proxy_hide_header x-pjax-url;

        proxy_set_header Host github.com;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Referer https://github.com/;
        proxy_set_header Origin https://github.com;
        #proxy_set_header Connection "";

        add_header x-pjax-url "https://hub.0z.gs$request_uri";

        proxy_http_version 1.1;
        proxy_connect_timeout 10s;
        proxy_read_timeout 10s;
        
        proxy_socket_keepalive on;
        
        proxy_ssl_server_name on;

        sub_filter "\"https://raw.githubusercontent.com" "\"https://raw.0z.gs";
        sub_filter "\"https://github.com" "\"https://hub.0z.gs";
        sub_filter "\"https://github.githubassets.com" "\"https://assets.0z.gs";
        sub_filter "\"https://github.githubassets.com" "\"https://assets.0z.gs";
        sub_filter "https://customer-stories-feed.github.com" "https://customer-stories-feed.0z.gs";
        sub_filter_once off;
        
        proxy_cookie_domain github.com hub.0z.gs;

        proxy_redirect https://github.com https://hub.0z.gs;
        proxy_redirect https://raw.githubusercontent.com https://raw.0z.gs;
        proxy_redirect https://github.githubassets.com https://assets.0z.gs;
        proxy_redirect https://customer-stories-feed.github.com https://customer-stories-feed.0z.gs;
        proxy_redirect https://codeload.github.com https://codeload.0z.gs;
        
        proxy_pass https://github;

    }
    

    # Block search engine
    # if ($http_user_agent ~* "qihoobot|Baiduspider|Bingbot|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
    # {
    #     return 403;
    # }

    # Anti Agent Bot DDoS
    # If behind CDN, use folloing commented code
    # if ($http_x_forwarded_for != $remote_addr) {
    #     return 503;
    # }
    
    if ($proxy_add_x_forwarded_for != $remote_addr) {
        return 503;
    }
    
    
    access_log  /web/logs/github.0z.gs.log;
    error_log  /www/logs/github.0z.gs.error.log;
}



因最近需要召开很多大会,GitHub 镜像站计划在 2022-09-21 23:59:59 UTC+8 暂时关闭服务,江湖路远,有缘再见!

如果需要自行部署为公共服务,这里提供一些数据参考,自 2021-12-11 到 2022-09-21 关闭服务,关站的最后一段时间,每日流量大约消耗 1.2 TiB,一个月 消耗 35 TiB + 流量,常驻网络带宽占用为 300 Mbps +,cpu 平均负载 1.7,服务器配置要求不是很高,cpu、硬盘 io 基本没什么要求,主要压力还是在网络带宽。


使用须知

  1. 服务端不保存任何日志和修改转发过程,仅是单纯流量转发(用户 → 镜像站 → GitHub 源站),有隐私、安全担忧者请勿登录镜像站
  2. 纯用爱发电,随时下线,无可用性保证,无速度保证

  • 感谢这个伟大的项目:https://github.com/FastGitORG。根据该 GitHub 项目部署了一个使用 Nginx 反向代理实现的 GitHub 镜像站

可以通过镜像站加速 GitHub 绝大部分内容 , 全程无感知,使用习惯跟原来保持一致,镜像站支持登录账号,如下图所示
请输入图片描述

  • 镜像列表:

    GitHub 域名镜像域名备注
    github.comhub.0z.gs
    raw.githubusercontent.comraw.0z.gs
    download.github.comdownload.0z.gs
    archive.github.comarchive.0z.gs
    codeload.github.comcodeload.0z.gs
    assets.github.comassets.0z.gs
    customer-stories-feed.github.comcustomer-stories-feed.0z.gs

如果想加速下载/访问,简单替换域名部分为镜像地址,其余不变:

https://hub.0z.gs/ender-zhao/Clash-for-Windows_Chinese/releases/download/CFW-V0.20.6_CN/Clash.for.Windows-0.20.6-win.7z

如果需要加速全部 GitHub 访问(网页访问和下载) ,可以使用镜像站登录账号 : https://hub.0z.gs

  • 其他 Git 操作以此类推,简单替换相应原始 GitHub 域名为镜像域名即可。

限制:暂未实现 ssh 克隆支持,,佛系跟随上游项目更新。
增强:a>不限 git 操作的仓库/文件大小(原上游配置限制 2 GiB),但是请勿滥用,否则会加回去限制
b>所有 镜像域名支持 CORS 跨域,方便调用,所有 镜像域名的响应 header 已增加跨域参数支持

  • 未来功能:

    • 接入 Cloudflare CDN
    • 支持 SSH 克隆
  • 服务器网络:
    电信 163 国际出口,美国洛杉矶 MC 机房,联通 / 移动 友好线路;

电信闲时尚可,晚高峰自求多福,或者等待后续接入 Cloudflare


更新日志:

2022-08-31:最近需要召开很多大会,加上长期用爱发电承担服务器费用压力日渐增大,可能会 暂时/永久 关闭镜像站
已知问题:登录镜像站下载 GitHub action 里面的 Artifacts 附件会返回 404 错误

2022-8-12 修复一个 BUG,当项目包含任意 *.php 后缀的文件时,Nginx 反向代理会意外解析 php,返回 404 http 状态码

2022-5-13,已同步上游项目进度,现已支持 通过镜像站 登录 GitHub,登录镜像站之后无需手动替换域名,使用方式跟原版 GitHub 一样,全程静默替换反代域名至镜像域名,如下图所示,支持静默自动替换的范围见 文中的镜像列表 。


鸣谢:

https://github.com/FastGitORG/nginx-conf
https://doc.fastgit.org/zh-cn/
https://github.com/fastgitorg/document
https://fastgit.org/
评论 (11条)
  1. Kent

    我知道怎么搞了,只是项目上游没有codeload.github.com这个域名的设置,能分享一下吗?

    回复
    1. 量子涨落 博主
      @Kent

      我记得有啊,不过我很早就没关注了,自己留一下他们的动态

      回复
    2. Kent
      @Kent

      customer-stories-feed.github.com还有这个域名的设置

      回复
  2. Kent

    是不是去用宝塔的反向代理也可以?

    回复
    1. 量子涨落 博主
      @Kent

      只要是nginx都可以,具体请看项目上游

      回复
      1. kent
        @量子涨落

        上面server的那些代码放在Nginx的哪个文件里?

        回复
    2. Kent
      @Kent

      用宝塔的反向代理不能登录。还有在宝塔安装Nginx后还能再做这样的反向代理吗?

      回复
      1. 量子涨落 博主
        @Kent

        请参考项目上游

        回复
  3. kent

    不能再见啊!很好用的。或者写个教程怎么搭建啊。这么好的东西不能失传啊

    回复
  4. nnclub

    感谢!

    回复
  5. 不错

    回复