2018-06-27 11:08:09 +00:00
user nginx ;
worker_processes auto ;
2018-11-04 10:23:52 +00:00
# error log config comes from external file created by entrypoint, to toggle debug on/off.
include /etc/nginx/error.log.debug.warn ;
2018-06-27 11:08:09 +00:00
pid /var/run/nginx.pid ;
events {
worker_connections 1024 ;
}
http {
2018-07-03 09:19:29 +00:00
map_hash_bucket_size 128 ;
2018-06-27 11:08:09 +00:00
include /etc/nginx/mime.types ;
default_type application/octet-stream ;
2018-11-04 10:23:52 +00:00
# Use a debug-oriented logging format.
2018-06-28 23:39:02 +00:00
log_format debugging ' $remote_addr - $remote_user [ $time_local] " $request" '
2018-06-27 11:08:09 +00:00
'$status $body_bytes_sent '
'"HOST: $host" "UPSTREAM: $upstream_addr" '
'"UPSTREAM-STATUS: $upstream_status" '
'"SSL-PROTO: $ssl_protocol" '
2018-11-04 15:43:53 +00:00
'"CONNECT-HOST: $connect_host" "CONNECT-PORT: $connect_port" "CONNECT-ADDR: $connect_addr" '
'"PROXY-HOST: $proxy_host" "UPSTREAM-REDIRECT: $upstream_http_location" "CACHE-STATUS: $upstream_cache_status" '
'"AUTH: $http_authorization" ' ;
log_format debug_proxy 'CONNECTPROXY: $remote_addr - $remote_user [ $time_local] " $request" '
'$status $body_bytes_sent '
'"HOST: $host" "UPSTREAM: $upstream_addr" '
'"UPSTREAM-STATUS: $upstream_status" '
'"SSL-PROTO: $ssl_protocol" '
2018-11-04 10:23:52 +00:00
'"CONNECT-HOST: $connect_host" "CONNECT-PORT: $connect_port" "CONNECT-ADDR: $connect_addr" "INTERCEPTED: $interceptedHost" '
2018-06-28 23:39:02 +00:00
'"PROXY-HOST: $proxy_host" "UPSTREAM-REDIRECT: $upstream_http_location" "CACHE-STATUS: $upstream_cache_status" '
'"AUTH: $http_authorization" ' ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
log_format tweaked ' $upstream_cache_status [ $time_local] " $uri" '
'$status $body_bytes_sent '
'"HOST:$host" '
'"PROXY-HOST:$proxy_host" "UPSTREAM: $upstream_addr" ' ;
2018-11-04 10:23:52 +00:00
2018-06-27 11:08:09 +00:00
keepalive_timeout 300 ;
gzip off ;
# The cache directory. This can get huge. Better to use a Docker volume pointing here!
# Set to 32gb which should be enough
proxy_cache_path /docker_mirror_cache levels=1:2 max_size=32g inactive=60d keys_zone=cache:10m use_temp_path=off ;
2018-11-04 10:23:52 +00:00
2018-06-27 11:08:09 +00:00
# Just in case you want to rewrite some hosts. Default maps directly.
map $host $targetHost {
hostnames ;
default $host ;
}
2018-06-28 23:39:02 +00:00
# A map to enable authentication to some specific docker registries.
# This is auto-generated by the entrypoint.sh based on environment variables
2018-06-27 13:18:25 +00:00
map $host $dockerAuth {
2018-06-28 23:39:02 +00:00
hostnames ;
include /etc/nginx/docker.auth.map ;
2018-06-27 13:18:25 +00:00
default "" ;
}
2018-11-04 10:23:52 +00:00
2018-11-04 15:43:53 +00:00
# @TODO: actually for auth.docker.io, if we want to support multiple authentications, we'll need to decide
# @TODO: based not only on the hostname, but also URI (/token) and query string (?scope)
# @TODO: I wonder if this would help gcr.io and quay.io with authentication also....
map $dockerAuth $finalAuth {
"" " $http_authorization" ; # if empty, keep the original passed-in from the docker client.
default "Basic $dockerAuth" ; # if not empty, add the Basic preamble to the auth
}
2018-06-28 23:39:02 +00:00
# Map to decide which hosts get directed to the caching portion.
# This is automatically generated from the list of cached registries, plus a few fixed hosts
# By default, we don't intercept, allowing free flow of non-registry traffic
map $connect_host $interceptedHost {
hostnames ;
include /etc/nginx/docker.intercept.map ;
2018-11-04 15:43:53 +00:00
default " $connect_addr" ; # $connect_addr is 'IP address and port of the remote host, e.g. "192.168.1.5:12345". IP address is resolved from host name of CONNECT request line.'
2018-06-28 23:39:02 +00:00
}
2018-11-04 10:23:52 +00:00
2018-06-27 11:08:09 +00:00
# These maps parse the original Host and URI from a /forcecache redirect.
map $request_uri $realHost {
~/forcecacheinsecure/([^:/]+)/originalwas(/.+) $1 ;
~/forcecachesecure/([^:/]+)/originalwas(/.+) $1 ;
default "DID_NOT_MATCH_HOST" ;
}
map $request_uri $realPath {
~/forcecacheinsecure/([^:/]+)/originalwas(/.+) $2 ;
~/forcecachesecure/([^:/]+)/originalwas(/.+) $2 ;
default "DID_NOT_MATCH_PATH" ;
}
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# The proxy director layer, listens on 3128
server {
listen 3128 ;
server_name _ ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# dont log the CONNECT proxy.
2018-11-04 15:43:53 +00:00
#access_log /var/log/nginx/access.log debug_proxy;
2018-06-28 23:39:02 +00:00
access_log off ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
proxy_connect ;
proxy_connect_address $interceptedHost ;
proxy_max_temp_file_size 0 ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# We need to resolve the real names of our proxied servers.
2018-12-12 08:18:34 +00:00
#resolver 8.8.8.8 4.2.2.2 ipv6=off; # Avoid ipv6 addresses for now
include /etc/nginx/resolvers.conf ;
2018-06-28 23:39:02 +00:00
# forward proxy for non-CONNECT request
location / {
2018-11-04 15:43:53 +00:00
add_header "Content-type" "text/plain" always ;
return 200 "docker-registry-proxy: The docker caching proxy is working!" ;
2018-06-28 23:39:02 +00:00
}
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
location /ca.crt {
2018-11-04 10:23:52 +00:00
alias /ca/ca.crt ;
2018-06-28 23:39:02 +00:00
}
2018-12-14 20:18:28 +00:00
location /setup/systemd {
2018-12-12 08:42:40 +00:00
add_header "Content-type" "text/plain" always ;
return 200 '
2018-12-14 20:18:28 +00:00
set -e
if [ ! -d /etc/systemd ] ; then
echo "Not a systemd system"
exit 1
fi
if [[ $EUID -ne 0 ]] ; then
echo "Must be root to change system files"
exit 1
fi
if [[ $(systemctl is-active --quiet docker.service) -ne 0 ]] ; then
echo "Docker service missing"
exit 1
fi
2018-12-12 08:42:40 +00:00
mkdir -p /etc/systemd/system/docker.service.d
cat << EOD > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTPS_PROXY=$scheme://$http_host/"
EOD
# Get the CA certificate from the proxy and make it a trusted root.
curl $scheme://$http_host/ca.crt > /usr/share/ca-certificates/docker_registry_proxy.crt
2018-12-14 20:18:28 +00:00
if fgrep -q "docker_registry_proxy.crt" /etc/ca-certificates.conf ; then
echo "certificate refreshed"
else
echo "docker_registry_proxy.crt" >> /etc/ca-certificates.conf
fi
2018-12-12 08:42:40 +00:00
update-ca-certificates --fresh
# Reload systemd
systemctl daemon-reload
# Restart dockerd
systemctl restart docker.service
2018-12-14 20:18:28 +00:00
echo "Docker configured with HTTPS_PROXY= $scheme://$http_host/"
2018-12-12 08:42:40 +00:00
' ;
2018-12-14 20:18:28 +00:00
} # end location /setup/systemd
} # end server
2018-11-04 10:23:52 +00:00
2018-06-27 11:08:09 +00:00
2018-06-28 23:39:02 +00:00
# The caching layer
2018-06-27 11:08:09 +00:00
server {
# Listen on both 80 and 443, for all hostnames.
2018-11-04 10:23:52 +00:00
# actually could be 443 or 444, depending on debug. this is now generated by the entrypoint.
2018-06-27 11:08:09 +00:00
listen 80 default_server ;
2018-11-04 10:23:52 +00:00
include /etc/nginx/caching.layer.listen ;
2018-06-27 11:08:09 +00:00
server_name _ ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# Do some tweaked logging.
access_log /var/log/nginx/access.log tweaked ;
2018-06-27 11:08:09 +00:00
2018-06-28 23:39:02 +00:00
# Use the generated certificates, they contain names for all the proxied registries.
ssl_certificate /certs/fullchain.pem ;
ssl_certificate_key /certs/web.key ;
2018-06-27 11:08:09 +00:00
# We need to resolve the real names of our proxied servers.
2018-12-12 08:18:34 +00:00
#resolver 8.8.8.8 4.2.2.2 ipv6=off; # Avoid ipv6 addresses for now
include /etc/nginx/resolvers.conf ;
2018-06-27 11:08:09 +00:00
# Docker needs this. Don't ask.
chunked_transfer_encoding on ;
# Block POST/PUT/DELETE. Don't use this proxy for pushing.
if ( $request_method = POST) {
2018-06-28 23:39:02 +00:00
return 405 "POST method is not allowed" ;
2018-06-27 11:08:09 +00:00
}
if ( $request_method = PUT) {
2018-06-28 23:39:02 +00:00
return 405 "PUT method is not allowed" ;
2018-06-27 11:08:09 +00:00
}
if ( $request_method = DELETE) {
2018-06-28 23:39:02 +00:00
return 405 "DELETE method is not allowed" ;
2018-06-27 11:08:09 +00:00
}
2018-11-04 10:23:52 +00:00
2018-06-27 11:08:09 +00:00
proxy_read_timeout 900 ;
2018-11-04 10:23:52 +00:00
# Use cache locking, with a huge timeout, so that multiple Docker clients asking for the same blob at the same time
2018-06-27 11:08:09 +00:00
# will wait for the first to finish instead of doing multiple upstream requests.
proxy_cache_lock on ;
2018-11-04 15:43:53 +00:00
proxy_cache_lock_timeout 880s ;
2018-06-27 13:18:25 +00:00
# Cache all 200, 301, 302, and 307 (emitted by private registries) for 60 days.
2018-11-04 10:23:52 +00:00
proxy_cache_valid 200 301 302 307 60d ;
2018-06-27 13:18:25 +00:00
# Some extra settings to maximize cache hits and efficiency
2018-06-27 11:08:09 +00:00
proxy_force_ranges on ;
proxy_ignore_client_abort on ;
proxy_cache_revalidate on ;
2018-06-27 13:18:25 +00:00
# Hide/ignore headers from caching. S3 especially likes to send Expires headers in the past in some situations.
proxy_hide_header Set-Cookie ;
2018-06-28 23:39:02 +00:00
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# Add the authentication info, if the map matched the target domain.
proxy_set_header Authorization $finalAuth ;
2018-11-04 10:23:52 +00:00
2018-06-28 23:39:02 +00:00
# This comes from a include file generated by the entrypoint.
include /etc/nginx/docker.verify.ssl.conf ;
2018-11-04 10:23:52 +00:00
2018-06-27 13:18:25 +00:00
# Block API v1. We dont know how to handle these.
# Docker-client should start with v2 and fallback to v1 if something fails, for example, if authentication failed to a protected v2 resource.
location /v1 {
2018-11-04 15:43:53 +00:00
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" ;
2018-06-27 13:18:25 +00:00
}
2018-06-27 11:08:09 +00:00
2018-11-04 15:43:53 +00:00
# for the /v2/..../blobs/.... URIs, do cache, and treat redirects.
location ~ ^/v2/(.*)/blobs/ {
2018-06-27 11:08:09 +00:00
proxy_pass https:// $targetHost ;
proxy_cache cache ;
2018-11-04 15:43:53 +00:00
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:blobs" ;
2018-06-27 11:08:09 +00:00
# Handling of redirects.
# Many registries (eg, quay.io, or k8s.gcr.io) emit a Location redirect
# pointing to something like cloudfront, or google storage.
# We hack into the response, extracting the host and URI parts, injecting them into a URL that points back to us
# That gives us a chance to intercept and cache those, which are the actual multi-megabyte blobs we originally wanted to cache.
# We to it twice, one for http and another for https.
2018-06-28 23:39:02 +00:00
proxy_redirect ~ ^https://([^:/]+)(/.+)$ https://docker.caching.proxy.internal/forcecachesecure/ $1/originalwas$2 ;
proxy_redirect ~ ^http://([^:/]+)(/.+)$ http://docker.caching.proxy.internal/forcecacheinsecure/ $1/originalwas$2 ;
2018-06-27 11:08:09 +00:00
}
2018-11-04 15:43:53 +00:00
2018-06-27 11:08:09 +00:00
# handling for the redirect case explained above, with https.
# The $realHost and $realPath variables come from a map defined at the top of this file.
location /forcecachesecure {
proxy_pass https:// $realHost$realPath ;
proxy_cache cache ;
2018-11-04 10:23:52 +00:00
2018-06-27 13:18:25 +00:00
# Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
proxy_cache_key $proxy_host$uri ;
2018-11-04 10:23:52 +00:00
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecachesecure" ;
2018-06-27 11:08:09 +00:00
}
# handling for the redirect case explained above, with http.
# The $realHost and $realPath variables come from a map defined at the top of this file.
location /forcecacheinsecure {
proxy_pass http:// $realHost$realPath ;
proxy_cache cache ;
2018-11-04 10:23:52 +00:00
2018-06-27 13:18:25 +00:00
# Change the cache key, so that we can cache signed S3 requests and such. Only host and path are considered.
proxy_cache_key $proxy_host$uri ;
2018-11-04 10:23:52 +00:00
add_header X-Docker-Caching-Proxy-Debug-Cache "yes:forcecacheinsecure" ;
2018-06-27 11:08:09 +00:00
}
2018-11-04 15:43:53 +00:00
# by default, dont cache anything.
location / {
proxy_pass https:// $targetHost ;
proxy_cache off ;
add_header X-Docker-Caching-Proxy-Debug-Cache "no:default" ;
}
2018-06-27 11:08:09 +00:00
}
2018-12-12 08:18:34 +00:00
}