11user nginx;
22worker_processes auto;
33
4- error_log /var/log/nginx/error.log warn;
4+ # error log config comes from external file created by entrypoint, to toggle debug on/off.
5+ include /etc/nginx/error.log.debug.warn;
6+
57pid /var/run/nginx.pid ;
68
79events {
@@ -13,28 +15,28 @@ http {
1315 include /etc/nginx/mime.types ;
1416 default_type application/octet-stream ;
1517
16- # Use a debug-oriented logging format.
18+ # Use a debug-oriented logging format.
1719 log_format debugging '$remote_addr - $remote_user [$time_local] "$request" '
1820 '$status $body_bytes_sent '
1921 '"HOST: $host" "UPSTREAM: $upstream_addr" '
2022 '"UPSTREAM-STATUS: $upstream_status" '
2123 '"SSL-PROTO: $ssl_protocol" '
22- '"CONNECT-HOST: $connect_host" "CONNECT-PORT: $connect_port" "CONNECT-ADDR: $connect_addr" '
24+ '"CONNECT-HOST: $connect_host" "CONNECT-PORT: $connect_port" "CONNECT-ADDR: $connect_addr" "INTERCEPTED: $interceptedHost" '
2325 '"PROXY-HOST: $proxy_host" "UPSTREAM-REDIRECT: $upstream_http_location" "CACHE-STATUS: $upstream_cache_status" '
2426 '"AUTH: $http_authorization" ' ;
25-
27+
2628 log_format tweaked '$upstream_cache_status [$time_local] "$uri" '
2729 '$status $body_bytes_sent '
2830 '"HOST:$host" '
2931 '"PROXY-HOST:$proxy_host" "UPSTREAM:$upstream_addr" ' ;
30-
32+
3133 keepalive_timeout 300 ;
3234 gzip off;
3335
3436 # The cache directory. This can get huge. Better to use a Docker volume pointing here!
3537 # Set to 32gb which should be enough
3638 proxy_cache_path /docker_mirror_cache levels=1:2 max_size=32g inactive=60d keys_zone=cache:10m use_temp_path=off;
37-
39+
3840 # Just in case you want to rewrite some hosts. Default maps directly.
3941 map $host $targetHost {
4042 hostnames;
4850 include /etc/nginx/docker.auth.map ;
4951 default "" ;
5052 }
51-
53+
5254 # Map to decide which hosts get directed to the caching portion.
5355 # This is automatically generated from the list of cached registries, plus a few fixed hosts
5456 # By default, we don't intercept, allowing free flow of non-registry traffic
@@ -57,13 +59,13 @@ http {
5759 include /etc/nginx/docker.intercept.map ;
5860 default "$connect_host:443" ;
5961 }
60-
61- map $dockerAuth $finalAuth {
62+
63+ map $dockerAuth $finalAuth {
6264 "" "$http_authorization" ; # if empty, keep the original passed-in from the client
6365 default "Basic $dockerAuth" ; # if not empty, add the Basic preamble to the auth
6466 }
65-
66-
67+
68+
6769 # These maps parse the original Host and URI from a /forcecache redirect.
6870 map $request_uri $realHost {
6971 ~ /forcecacheinsecure/( [^:/]+) /originalwas( /.+) $1 ;
@@ -76,43 +78,44 @@ http {
7678 ~ /forcecachesecure/( [^:/]+) /originalwas( /.+) $2 ;
7779 default "DID_NOT_MATCH_PATH" ;
7880 }
79-
80-
81+
82+
8183 # The proxy director layer, listens on 3128
8284 server {
8385 listen 3128 ;
8486 server_name _;
85-
87+
8688 # dont log the CONNECT proxy.
8789 access_log off;
88-
90+
8991 proxy_connect;
9092 proxy_connect_address $interceptedHost ;
9193 proxy_max_temp_file_size 0;
92-
94+
9395 # We need to resolve the real names of our proxied servers.
9496 resolver 8.8.8.8 4.2.2.2 ipv6=off; # Avoid ipv6 addresses for now
9597
9698 # forward proxy for non-CONNECT request
9799 location / {
98100 return 403 "The docker caching proxy is working!" ;
99101 }
100-
102+
101103 location /ca.crt {
102- alias /ca/ca.crt;
104+ alias /ca/ca.crt;
103105 }
104106
105107 # @TODO: add a dynamic root path that generates instructions for usage on docker clients
106108 }
107-
109+
108110
109111 # The caching layer
110112 server {
111113 # Listen on both 80 and 443, for all hostnames.
114+ # actually could be 443 or 444, depending on debug. this is now generated by the entrypoint.
112115 listen 80 default_server;
113- listen 443 ssl default_server ;
116+ include /etc/nginx/caching.layer. listen ;
114117 server_name _;
115-
118+
116119 # Do some tweaked logging.
117120 access_log /var/log/nginx/access.log tweaked;
118121
@@ -136,17 +139,17 @@ http {
136139 if ( $request_method = DELETE) {
137140 return 405 "DELETE method is not allowed" ;
138141 }
139-
142+
140143 proxy_read_timeout 900 ;
141-
142- # Use cache locking, with a huge timeout, so that multiple Docker clients asking for the same blob at the same time
144+
145+ # Use cache locking, with a huge timeout, so that multiple Docker clients asking for the same blob at the same time
143146 # will wait for the first to finish instead of doing multiple upstream requests.
144147 proxy_cache_lock on;
145148 proxy_cache_lock_timeout 120s ;
146149
147150 # Cache all 200, 301, 302, and 307 (emitted by private registries) for 60 days.
148- proxy_cache_valid 200 301 302 307 60d ;
149-
151+ proxy_cache_valid 200 301 302 307 60d ;
152+
150153 # Some extra settings to maximize cache hits and efficiency
151154 proxy_force_ranges on;
152155 proxy_ignore_client_abort on;
@@ -155,13 +158,13 @@ http {
155158 # Hide/ignore headers from caching. S3 especially likes to send Expires headers in the past in some situations.
156159 proxy_hide_header Set-Cookie;
157160 proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
158-
161+
159162 # Add the authentication info, if the map matched the target domain.
160163 proxy_set_header Authorization $finalAuth ;
161-
164+
162165 # This comes from a include file generated by the entrypoint.
163166 include /etc/nginx/docker.verify.ssl .conf;
164-
167+
165168 # Some debugging info
166169 # add_header X-Docker-Caching-Proxy-Real-Host $realHost;
167170 # add_header X-Docker-Caching-Proxy-Real-Path $realPath;
@@ -176,23 +179,35 @@ http {
176179 # don't cache mutable entity /v2/<name>/manifests/<reference> (unless the reference is a digest)
177180 location ~ ^/v2/[^\/]+/manifests/(?![A-Fa-f0-9_+.-]+:) {
178181 proxy_pass https://$targetHost ;
182+ add_header X-Docker-Caching-Proxy-Debug-Cache "no:manifests" ;
179183 }
180184
181185 # don't cache mutable entity /v2/<name>/tags/list
182186 location ~ ^/v2/[^\/]+/tags/list {
183187 proxy_pass https://$targetHost ;
188+ proxy_cache off;
189+ add_header X-Docker-Caching-Proxy-Debug-Cache "no:tagslist" ;
184190 }
185191
186192 # don't cache mutable entity /v2/_catalog
187193 location ~ ^/v2/_catalog$ {
188194 proxy_pass https://$targetHost ;
195+ proxy_cache off;
196+ add_header X-Docker-Caching-Proxy-Debug-Cache "no:catalog" ;
189197 }
190-
191- # force cache of the first hit which is always /v2/ - even for 401 unauthorized.
198+
199+ # dont cache the first hit which is always /v2/
192200 location = /v2/ {
193201 proxy_pass https://$targetHost ;
194- proxy_cache cache;
195- proxy_cache_valid 200 301 302 307 401 60d ;
202+ proxy_cache off;
203+ add_header X-Docker-Caching-Proxy-Debug-Cache "no:rootv2" ;
204+ }
205+
206+ # dont cache /token (done against auth servers)
207+ location = /token {
208+ proxy_pass https://$targetHost ;
209+ proxy_cache off;
210+ add_header X-Docker-Caching-Proxy-Debug-Cache "no:token" ;
196211 }
197212
198213 # cache everything else
@@ -208,26 +223,33 @@ http {
208223 # We to it twice, one for http and another for https.
209224 proxy_redirect ~ ^https://( [^:/]+)( /.+) $ https://docker.caching.proxy.internal /forcecachesecure/$1 /originalwas$2 ;
210225 proxy_redirect ~ ^http ://( [^:/]+)( /.+) $ http ://docker.caching.proxy.internal /forcecacheinsecure/$1 /originalwas$2 ;
226+
227+ add_header X-Docker-Caching-Proxy-Debug-Cache "yes:everythingelse" ;
211228 }
212229
213230 # handling for the redirect case explained above, with https.
214231 # The $realHost and $realPath variables come from a map defined at the top of this file.
215232 location /forcecachesecure {
216233 proxy_pass https://$realHost$realPath ;
217234 proxy_cache cache;
218-
235+
219236 # Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
220237 proxy_cache_key $proxy_host$uri ;
238+
239+ add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecachesecure" ;
240+
221241 }
222242
223243 # handling for the redirect case explained above, with http.
224244 # The $realHost and $realPath variables come from a map defined at the top of this file.
225245 location /forcecacheinsecure {
226246 proxy_pass http ://$realHost$realPath ;
227247 proxy_cache cache;
228-
248+
229249 # Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
230250 proxy_cache_key $proxy_host$uri ;
251+
252+ add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecacheinsecure" ;
231253 }
232254 }
233255}
0 commit comments