3

用Nginx反代Github加速Git

 1 year ago
source link: https://www.taterli.com/9197/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

用Nginx反代Github加速Git

用Nginx反代Github加速Git

如果反代整个GitHub风险太大,而且可能会被拿去干坏事,所以,这里打算只反代Git客户端访问GitHub,为了方便很多国内无法通过代理或者不方便通过代理的人.另外,这个建议进行白名单IP限制,避免滥用,比如我反代后再套CF,再通过CF限制访问来源.

参考配置.

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name _;

    # SSL 相关配置,这里不展开!
    include /etc/cloudflare/nginx.conf;

    # Git 客户端对于HTTPS请求只有这两个方法
    add_header Allow "GET, POST" always;

    # 不需要记录日志
    access_log off;
    log_not_found off;

    if ($request_method !~ ^(GET|POST)$) { # 访问方法不对也不可以
        return 405;
    }

    if ($http_user_agent !~* (git)) { # 只有用Git客户端访问才可以
        return 403;
    }

    access_log /var/log/nginx/access.log;

    # 只有两个URL可以访问
    location ~ (^/[^/]+/[^/]+/git-upload-pack|^/[^/]+/[^/]+/info/refs) {
        proxy_set_header Accept-Encoding "";
        proxy_set_header Connection      "";
        proxy_http_version               1.1;
        proxy_connect_timeout            10s;
        proxy_read_timeout               10s;
        proxy_set_header Host            github.com;

        # 防止HSTS校验问题
        proxy_hide_header Strict-Transport-Security;
        
        # 上游就是GitHub
        proxy_pass https://github.com;
    }

    location / {
        return 403;
    }
}

用法,比如原来是 https://github.com/espressif/esp-idf

那么修改后是 https://github.example.com/espressif/esp-idf

实际测试一下,速度还是飞快的,当然取决于服务器本身,不过我这个测试目前是套着CF的,速度也是很好,递归克隆也没问题.

image-28.png

这个反代方法只能用Git客户端Clone,没法进行其他操作,上传代码也不行,都有条件上传了,你会没条件直接去GitHub?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK