menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

nginx反向代理加速github克隆速度

作者:Chancel Yang, 创建:2023-02-13, 字数:2041, 已阅:47, 最后更新:2024-03-10

国内对github等网站的干扰是越来越严重了,部分服务器上也不合适安装转发流量的工具,遇到拷贝克隆github的库真是一头包

于是考虑使用nginx反代来加速仓库克隆,也顺带支持一些常见的如debian源的更新

nginx反代配置文件参考如下

TEXT
server {
    server_name proxy.chancel.me;

    access_log  /var/log/nginx/proxy.log;
    error_log   /var/log/nginx/proxy.log;

    location / {
        return 404;
    }

    # 禁止访问配置文件
    location ~* .(conf|sql|bak|ini|yaml)$ {
        deny all;
    }

    # 指定允许的http method
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }

    # 排除机器人访问
    if ($http_user_agent ~* "qihoobot|Baiduspider|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;
    }

    # 正则匹配需要反向代理的网站
    location ~ ^/(www\.debian\.org|deb\.debian\.org|security\.debian\.org|github\.com)(/.*)$ {
        resolver 8.8.8.8 8.8.4.4 valid=600s;
        resolver_timeout 10s;
        proxy_pass_request_headers on;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_hide_header Strict-Transport-Security;
        set $query_mark "";
        if ($query_string != "") {
            set $query_mark "?${query_string}";
        }
        proxy_pass $scheme://$1$2$query_mark;
    }

    # 引入https设置
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/chancel.me/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/chancel.me/privkey.pem; # managed by Certbot

}


server{
    if ($host = proxy.chancel.me) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name proxy.chancel.me;
    return 404; # managed by Certbot
}

设置完成后,如克隆cefsharp的github仓库,原本是

Bash
git clone https://github.com/cefsharp/CefSharp.git

只需要改成以下的形式(替换域名,其他的不变)

Bash
git clone https://proxy.chancel.me/github.com/cefsharp/CefSharp.git

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]