docker hub 自建镜像加速

在拉取镜像时发现之前使用的代理已经失效了,而且 auth.docker.io 也被墙,导致网上很多教程都失效了,搜索到一个可用的就写下来当作备忘录。

有两种自建方案,一个是 nginx 反代,一个是使用 CloudFlare Worker 搭建。

nginx:

location /v2/ {
proxy_pass https://registry-1.docker.io; # Docker Hub 的官方镜像仓库
proxy_set_header Host registry-1.docker.io;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 关闭缓存
proxy_buffering off;
# 转发认证相关的头部
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
# 重写 www-authenticate 头为你的反代地址
proxy_hide_header www-authenticate;
add_header www-authenticate ‘Bearer realm=”https://xxx/token”,service=”registry.docker.io”‘ always;
# always 参数确保该头部在返回 401 错误时无论什么情况下都会被添加。
# 对 upstream 状态码检查,实现 error_page 错误重定向
proxy_intercept_errors on;
# error_page 指令默认只检查了第一次后端返回的状态码,开启后可以跟随多次重定向。
recursive_error_pages on;
# 根据状态码执行对应操作,以下为301、302、307状态码都会触发
error_page 301 302 307 = @handle_redirect;
}
# 处理 Docker OAuth2 Token 认证请求
location /token {
resolver 8.8.8.8 valid=600s;
proxy_pass https://auth.docker.io; # Docker 认证服务器
# 设置请求头,确保转发正确
proxy_set_header Host auth.docker.io;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 传递 Authorization 头信息,获取 Token
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
# 禁用缓存
proxy_buffering off;
}
location @handle_redirect {
resolver 8.8.8.8;
set $saved_redirect_location ‘$upstream_http_location’;
proxy_pass $saved_redirect_location;
}

 

CloudFlare Worker:

https://github.com/cmliu/CF-Workers-docker.io

原创文章,作者:mantou,如若转载,请注明出处:https://v2ez.com/1547.html

(0)
mantoumantou
上一篇 6天前
下一篇 2022年4月5日

相关推荐

发表回复

登录后才能评论