Bboysoul's Blog

首页 公告 RSS

使用nginx反向代理github加速clone

March 12, 2020 本文有 683 个字 需要花费 2 分钟阅读

简介

没有什么比网速慢更恐怖的事情了,尤其是在clone的时候,有时候真的是要好久

所以我就想到了使用nginx反向代理github

操作

首先你要搭建一个nginx

我一般会选择docker搭建nginx,然后使用docker-compose来启动,下面是我的nginx docker-compose 配置文件

https://github.com/bboysoulcn/awesome-dockercompose/tree/master/nginx

差不多就是上面这个样子,欢迎star fork上面这个项目

之后修改nginx的配置文件,我的配置文件如下

upstream github {
    server github.com:443;
    keepalive 16;
}

server {
    listen 80;
    server_name 你的域名;
    rewrite ^ https://$http_host$request_uri? permanent;
    access_log /var/log/nginx/github/https_ip.log;
    error_log  /var/log/nginx/github/https_ip_error.log;
}

server
{
    listen 443;
    server_name 你的域名;
    ssl_certificate   /cert/github/fullchain1.pem;
    ssl_certificate_key  /cert/github/privkey1.pem;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    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 / {
        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;

        proxy_hide_header Strict-Transport-Security; #隐藏协议头,避免因为反向代理开启hsts

        proxy_pass https://github;
    }


    access_log /var/log/nginx/github/https_ip.log;
    error_log  /var/log/nginx/github/https_ip_error.log;
}

接下来解释下上面的配置

upstream github {
    server github.com:443;
    keepalive 16;
}

这里upstream 建议填写域名,为什么呢,网上有的是写ip的,但是如果你写ip的话因为github每隔地区的节点ip是不一样的,比如你的服务器是在日本的,但是你写了一个韩国的github ip地址,这就会造成反向代理变得缓慢

server {
    listen 80;
    server_name 你的域名;
    rewrite ^ https://$http_host$request_uri? permanent;
    access_log /var/log/nginx/github/https_ip.log;
    error_log  /var/log/nginx/github/https_ip_error.log;
}

这里的rewrite就是为了强制跳转到https

    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;
    }

这里是为了防止搜索引擎收入这个地址,造成地址外泄

其他的配置没有什么可以说的,保持和我一致即可

最后,因为这个只是一个反向代理,所以只能当你碰到比如clone一个项目慢的时候使用

比如当你下载下面这个项目

git clone https://github.com/bboysoulcn/awesome-dockercompose.git

为了加快clone速度,你可以改为你的域名

git clone https://www.example.com/bboysoulcn/awesome-dockercompose.git

使用,但是你想完全当一个镜像站使用,那是不可能的。

欢迎关注我的博客www.bboy.app

Have Fun


Tags:

本站总访问量 本站总访客数