新锐公共CDN加速计划,欢迎大家使用!~
       查看详情

nginx反向代理github

前言

因为众所周知的缘故,github时常访问不了,于是就想着反代一下,刚好有台华为云的香港服务器,于是火毅盾的负责人就给了我以下代码

此反代除了登录其他基本没啥问题

教程

1.打开宝塔面板,找到网站的反向代理,设置代理地址为https://github.com,发送域名为$host,然后确定之后可以看到有个配置文件,将如下代码放进去

location ^~ / {
    # 将请求代理到 GitHub
    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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;

    # 清空 Accept-Encoding 头,防止后端返回压缩内容
    proxy_set_header Accept-Encoding "";

    # 替换响应内容中的字符串
    sub_filter "github.githubassets.com" "assets-github.xrbk.cn";
    sub_filter "github.com" "github.xrbk.cn";
    sub_filter "api.github.com" "api-github.xrbk.cn";
    sub_filter 'raw.githubusercontent.com' 'raw.github.xrbk.cn';
    sub_filter_once off;
    sub_filter_types *;

    # 处理 302 重定向中的地址替换
    proxy_redirect ~^https://raw.githubusercontent.com(.*)$ https://raw.github.xrbk.cn$1;

    # 隐藏后端服务器返回的 Content-Security-Policy 头
    proxy_hide_header Content-Security-Policy;

    # 添加自定义的 Content-Security-Policy 头
    add_header Content-Security-Policy "default-src 'self'; img-src *; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'";

    # 静态文件缓存设置
    set $static_fileHbPPNEua 0;
    if ($uri ~* ".(gif|png|jpg|css|js|woff|woff2)$") {
        set $static_fileHbPPNEua 1;
        expires 1m;
    }
    if ($static_fileHbPPNEua = 0) {
        add_header Cache-Control no-cache;
    }
}

2.其中为xrbk.cn的后缀都需要更改为你的域名

3.在宝塔的网站里比如github.xrbk.cn中再添加其他例如api-github.xrbk.cn的域名

4.去服务商那解析这几个子域名即可

© 版权声明
THE END
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容