Skip to content

Commit 2236393

Browse files
feat(nginx): cache by blob sha instead of uri (#34)
* enable caching by sha for blobs * add request method; disable head conversion * add cache key header, use original cache key var in redirects
1 parent be30eac commit 2236393

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ echo -n "" >/etc/nginx/nginx.manifest.caching.config.conf
153153
# First tier caching of manifests; configure via MANIFEST_CACHE_PRIMARY_REGEX and MANIFEST_CACHE_PRIMARY_TIME
154154
location ~ ^/v2/(.*)/manifests/${MANIFEST_CACHE_PRIMARY_REGEX} {
155155
set \$docker_proxy_request_type "manifest-primary";
156+
set \$cache_key \$uri;
156157
proxy_cache_valid ${MANIFEST_CACHE_PRIMARY_TIME};
157158
include "/etc/nginx/nginx.manifest.stale.conf";
158159
}
@@ -162,6 +163,7 @@ EOD
162163
# Secondary tier caching of manifests; configure via MANIFEST_CACHE_SECONDARY_REGEX and MANIFEST_CACHE_SECONDARY_TIME
163164
location ~ ^/v2/(.*)/manifests/${MANIFEST_CACHE_SECONDARY_REGEX} {
164165
set \$docker_proxy_request_type "manifest-secondary";
166+
set \$cache_key \$uri;
165167
proxy_cache_valid ${MANIFEST_CACHE_SECONDARY_TIME};
166168
include "/etc/nginx/nginx.manifest.stale.conf";
167169
}
@@ -171,6 +173,7 @@ EOD
171173
# Default tier caching for manifests. Caches for ${MANIFEST_CACHE_DEFAULT_TIME} (from MANIFEST_CACHE_DEFAULT_TIME)
172174
location ~ ^/v2/(.*)/manifests/ {
173175
set \$docker_proxy_request_type "manifest-default";
176+
set \$cache_key \$uri;
174177
proxy_cache_valid ${MANIFEST_CACHE_DEFAULT_TIME};
175178
include "/etc/nginx/nginx.manifest.stale.conf";
176179
}
@@ -180,6 +183,7 @@ EOD
180183
# Manifest caching is disabled. Enable it with ENABLE_MANIFEST_CACHE=true
181184
location ~ ^/v2/(.*)/manifests/ {
182185
set \$docker_proxy_request_type "manifest-default-disabled";
186+
set \$cache_key \$uri;
183187
proxy_cache_valid 0s;
184188
include "/etc/nginx/nginx.manifest.stale.conf";
185189
}

nginx.conf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
252252
proxy_ignore_client_abort on;
253253
proxy_cache_revalidate on;
254254

255+
# Avoid conversion of HEAD method to GET
256+
proxy_cache_convert_head off;
257+
255258
# Hide/ignore headers from caching. S3 especially likes to send Expires headers in the past in some situations.
256259
proxy_hide_header Set-Cookie;
257260
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
@@ -278,13 +281,15 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
278281
# For blob requests by digest, do cache, and treat redirects.
279282
location ~ ^/v2/(.*)/blobs/sha256:(.*) {
280283
set $docker_proxy_request_type "blob-by-digest";
284+
set $cache_key $request_method$2;
281285
include "/etc/nginx/nginx.manifest.common.conf";
282286
}
283287

284288
# For manifest requests by digest, do cache, and treat redirects.
285289
# These are some of the requests that DockerHub will throttle.
286290
location ~ ^/v2/(.*)/manifests/sha256:(.*) {
287291
set $docker_proxy_request_type "manifest-by-digest";
292+
set $cache_key $request_method$uri;
288293
include "/etc/nginx/nginx.manifest.common.conf";
289294
}
290295

@@ -297,6 +302,7 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
297302
# Since these are mutable, we invalidate them immediately and keep them only in case the backend is down
298303
location ~ ^/v2/(.*)/blobs/ {
299304
set $docker_proxy_request_type "blob-mutable";
305+
set $cache_key $request_method$uri;
300306
proxy_cache_valid 0s;
301307
include "/etc/nginx/nginx.manifest.stale.conf";
302308
}
@@ -322,7 +328,8 @@ echo "Docker configured with HTTPS_PROXY=$scheme://$http_host/"
322328
proxy_cache $cache;
323329
# But we store the result with the cache key of the original request URI
324330
# so that future clients don't need to follow the redirect too
325-
proxy_cache_key $original_uri$slice_range;
331+
proxy_cache_key $cache_key$slice_range;
332+
add_header X-Docker-Registry-Proxy-Cache-Key-Status "$cache_key$slice_range";
326333
}
327334

328335
# by default, dont cache anything.

nginx.manifest.common.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
proxy_pass https://$targetHost;
55
proxy_cache $cache;
66
slice 4m;
7-
proxy_cache_key $uri$slice_range;
7+
proxy_cache_key $cache_key$slice_range;
88
proxy_set_header Range $slice_range;
9+
add_header X-Docker-Registry-Proxy-Cache-Key-Status "$cache_key$slice_range";
910
proxy_http_version 1.1;
1011
proxy_intercept_errors on;
1112
error_page 301 302 307 = @handle_redirects;

0 commit comments

Comments
 (0)