Skip to content

Commit bc0ac35

Browse files
author
ricardop
committed
Merge branch 'pull/33'
2 parents ad75dce + e90d203 commit bc0ac35

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
clean:
2+
rm -rf docker_mirror_cache/*
3+
4+
build:
5+
docker build --tag docker-registry-proxy .
6+
7+
start:
8+
docker run --rm --name=docker-registry-proxy -it \
9+
-p 0.0.0.0:3128:3128 \
10+
-p 0.0.0.0:8081:8081 \
11+
-e DEBUG=true \
12+
-v $(dir $(abspath $(firstword $(MAKEFILE_LIST))))/docker_mirror_cache:/docker_mirror_cache \
13+
-v $(dir $(abspath $(firstword $(MAKEFILE_LIST))))/docker_mirror_certs:/ca \
14+
docker-registry-proxy
15+
16+
stop:
17+
docker stop docker-registry-proxy
18+
19+
test: build start
20+
21+
.INTERMEDIATE: clean stop

nginx.conf

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
196196
proxy_cache_lock on;
197197
proxy_cache_lock_timeout 880s;
198198

199-
# Cache all 200, 301, 302, and 307 (emitted by private registries) for 60 days.
200-
proxy_cache_valid 200 301 302 307 60d;
199+
# Cache all 200, 206 for 60 days.
200+
proxy_cache_valid 200 206 60d;
201201

202202
# Some extra settings to maximize cache hits and efficiency
203203
proxy_force_ranges on;
@@ -223,47 +223,28 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
223223
return 405 "docker-registry-proxy: docker is trying to use v1 API. Either the image does not exist upstream, or you need to configure docker-registry-proxy to authenticate against $host";
224224
}
225225

226-
227226
# for the /v2/..../blobs/.... URIs, do cache, and treat redirects.
228227
location ~ ^/v2/(.*)/blobs/ {
229228
proxy_pass https://$targetHost;
230229
proxy_cache cache;
231-
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:blobs";
232-
233-
# Handling of redirects.
234-
# Many registries (eg, quay.io, or k8s.gcr.io) emit a Location redirect
235-
# pointing to something like cloudfront, or google storage.
236-
# We hack into the response, extracting the host and URI parts, injecting them into a URL that points back to us
237-
# That gives us a chance to intercept and cache those, which are the actual multi-megabyte blobs we originally wanted to cache.
238-
# We to it twice, one for http and another for https.
239-
proxy_redirect ~^https://([^:/]+)(/.+)$ https://docker.caching.proxy.internal/forcecachesecure/$1/originalwas$2;
240-
proxy_redirect ~^http://([^:/]+)(/.+)$ http://docker.caching.proxy.internal/forcecacheinsecure/$1/originalwas$2;
230+
proxy_cache_key $uri;
231+
proxy_intercept_errors on;
232+
error_page 301 302 307 = @handle_redirects;
241233
}
242234

235+
location @handle_redirects {
236+
#store the current state of the world so we can reuse it in a minute
237+
# We need to capture these values now, because as soon as we invoke
238+
# the proxy_* directives, these will disappear
239+
set $original_uri $uri;
240+
set $orig_loc $upstream_http_location;
243241

244-
# handling for the redirect case explained above, with https.
245-
# The $realHost and $realPath variables come from a map defined at the top of this file.
246-
location /forcecachesecure {
247-
proxy_pass https://$realHost$realPath;
248-
proxy_cache cache;
249-
250-
# Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
251-
proxy_cache_key $proxy_host$uri;
252-
253-
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecachesecure";
254-
255-
}
256-
257-
# handling for the redirect case explained above, with http.
258-
# The $realHost and $realPath variables come from a map defined at the top of this file.
259-
location /forcecacheinsecure {
260-
proxy_pass http://$realHost$realPath;
242+
# nginx goes to fetch the value from the upstream Location header
243+
proxy_pass $orig_loc;
261244
proxy_cache cache;
262-
263-
# Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
264-
proxy_cache_key $proxy_host$uri;
265-
266-
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecacheinsecure";
245+
# But we store the result with the cache key of the original request URI
246+
# so that future clients don't need to follow the redirect too
247+
proxy_cache_key $original_uri;
267248
}
268249

269250
# by default, dont cache anything.
@@ -272,6 +253,5 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
272253
proxy_cache off;
273254
add_header X-Docker-Caching-Proxy-Debug-Cache "no:default";
274255
}
275-
276256
}
277257
}

0 commit comments

Comments
 (0)