completely reworked into an HTTPS_PROXY-based solution
- emit our own certificates - configurable via ENVs - generates config dinamically
This commit is contained in:
119
nginx.conf
119
nginx.conf
@@ -1,7 +1,7 @@
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
|
||||
error_log /var/log/nginx/error.log debug;
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
@@ -13,14 +13,20 @@ http {
|
||||
default_type application/octet-stream;
|
||||
|
||||
# Use a debug-oriented logging format.
|
||||
log_format tweaked '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
log_format debugging '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent '
|
||||
'"HOST: $host" "UPSTREAM: $upstream_addr" '
|
||||
'"UPSTREAM-STATUS: $upstream_status" '
|
||||
'"SSL-PROTO: $ssl_protocol" '
|
||||
'"PROXY-HOST: $proxy_host" "UPSTREAM-REDIRECT: $upstream_http_location" "CACHE-STATUS: $upstream_cache_status"';
|
||||
'"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 tweaked '$upstream_cache_status [$time_local] "$uri" '
|
||||
'$status $body_bytes_sent '
|
||||
'"HOST:$host" '
|
||||
'"PROXY-HOST:$proxy_host" "UPSTREAM:$upstream_addr" ';
|
||||
|
||||
access_log /var/log/nginx/access.log tweaked;
|
||||
keepalive_timeout 300;
|
||||
gzip off;
|
||||
|
||||
@@ -28,21 +34,35 @@ http {
|
||||
# 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;
|
||||
|
||||
|
||||
# Just in case you want to rewrite some hosts. Default maps directly.
|
||||
map $host $targetHost {
|
||||
hostnames;
|
||||
default $host;
|
||||
}
|
||||
|
||||
# A map to enable authentication to some specific docker hosts.
|
||||
# To use this, mount a volume in docker.
|
||||
|
||||
# A map to enable authentication to some specific docker registries.
|
||||
# This is auto-generated by the entrypoint.sh based on environment variables
|
||||
map $host $dockerAuth {
|
||||
include /etc/nginx/docker.auth.*.map;
|
||||
hostnames;
|
||||
include /etc/nginx/docker.auth.map;
|
||||
default "";
|
||||
}
|
||||
|
||||
# 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;
|
||||
default "$connect_host:443";
|
||||
}
|
||||
|
||||
map $dockerAuth $finalAuth {
|
||||
"" "$http_authorization"; # if empty, keep the original passed-in from the client
|
||||
default "Basic $dockerAuth"; # if not empty, add the Basic preamble to the auth
|
||||
}
|
||||
|
||||
|
||||
# These maps parse the original Host and URI from a /forcecache redirect.
|
||||
map $request_uri $realHost {
|
||||
~/forcecacheinsecure/([^:/]+)/originalwas(/.+) $1;
|
||||
@@ -55,16 +75,49 @@ http {
|
||||
~/forcecachesecure/([^:/]+)/originalwas(/.+) $2;
|
||||
default "DID_NOT_MATCH_PATH";
|
||||
}
|
||||
|
||||
|
||||
# The proxy director layer, listens on 3128
|
||||
server {
|
||||
listen 3128;
|
||||
server_name _;
|
||||
|
||||
# dont log the CONNECT proxy.
|
||||
access_log off;
|
||||
|
||||
proxy_connect;
|
||||
proxy_connect_address $interceptedHost;
|
||||
proxy_max_temp_file_size 0;
|
||||
|
||||
# We need to resolve the real names of our proxied servers.
|
||||
resolver 8.8.8.8 4.2.2.2 ipv6=off; # Avoid ipv6 addresses for now
|
||||
|
||||
# forward proxy for non-CONNECT request
|
||||
location / {
|
||||
return 403 "The docker caching proxy is working!";
|
||||
}
|
||||
|
||||
location /ca.crt {
|
||||
alias /ca/ca.crt;
|
||||
}
|
||||
|
||||
# @TODO: add a dynamic root path that generates instructions for usage on docker clients
|
||||
}
|
||||
|
||||
|
||||
# The caching layer
|
||||
server {
|
||||
# Listen on both 80 and 443, for all hostnames.
|
||||
listen 80 default_server;
|
||||
listen 443 ssl default_server;
|
||||
server_name _;
|
||||
|
||||
# Do some tweaked logging.
|
||||
access_log /var/log/nginx/access.log tweaked;
|
||||
|
||||
# Use a fake SSL certificate. This does not matter, since the Docker clients will be configured with insecure registry
|
||||
ssl_certificate /etc/ssl/cert.pem;
|
||||
ssl_certificate_key /etc/ssl/key.pem;
|
||||
# Use the generated certificates, they contain names for all the proxied registries.
|
||||
ssl_certificate /certs/fullchain.pem;
|
||||
ssl_certificate_key /certs/web.key;
|
||||
|
||||
# We need to resolve the real names of our proxied servers.
|
||||
resolver 8.8.8.8 4.2.2.2 ipv6=off; # Avoid ipv6 addresses for now
|
||||
@@ -74,13 +127,13 @@ http {
|
||||
|
||||
# Block POST/PUT/DELETE. Don't use this proxy for pushing.
|
||||
if ($request_method = POST) {
|
||||
return 405;
|
||||
return 405 "POST method is not allowed";
|
||||
}
|
||||
if ($request_method = PUT) {
|
||||
return 405;
|
||||
return 405 "PUT method is not allowed";
|
||||
}
|
||||
if ($request_method = DELETE) {
|
||||
return 405;
|
||||
return 405 "DELETE method is not allowed";
|
||||
}
|
||||
|
||||
proxy_read_timeout 900;
|
||||
@@ -100,12 +153,23 @@ http {
|
||||
|
||||
# Hide/ignore headers from caching. S3 especially likes to send Expires headers in the past in some situations.
|
||||
proxy_hide_header Set-Cookie;
|
||||
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
|
||||
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
|
||||
|
||||
# Add the authentication info, if the map matched the target domain.
|
||||
proxy_set_header Authorization $finalAuth;
|
||||
|
||||
# This comes from a include file generated by the entrypoint.
|
||||
include /etc/nginx/docker.verify.ssl.conf;
|
||||
|
||||
# Some debugging info
|
||||
# add_header X-Docker-Caching-Proxy-Real-Host $realHost;
|
||||
# add_header X-Docker-Caching-Proxy-Real-Path $realPath;
|
||||
# add_header X-Docker-Caching-Proxy-Auth $finalAuth;
|
||||
|
||||
# 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 {
|
||||
return 405;
|
||||
return 405 "API v1 is invalid -- you probably need auth to get a v2 endpoint working against $host -- Check the docs";
|
||||
}
|
||||
|
||||
# don't cache mutable entity /v2/<name>/manifests/<reference> (unless the reference is a digest)
|
||||
@@ -122,6 +186,13 @@ http {
|
||||
location ~ ^/v2/_catalog$ {
|
||||
proxy_pass https://$targetHost;
|
||||
}
|
||||
|
||||
# force cache of the first hit which is always /v2/ - even for 401 unauthorized.
|
||||
location = /v2/ {
|
||||
proxy_pass https://$targetHost;
|
||||
proxy_cache cache;
|
||||
proxy_cache_valid 200 301 302 307 401 60d;
|
||||
}
|
||||
|
||||
# cache everything else
|
||||
location / {
|
||||
@@ -134,8 +205,8 @@ http {
|
||||
# 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.
|
||||
proxy_redirect ~^https://([^:/]+)(/.+)$ https://docker.proxy/forcecachesecure/$1/originalwas$2;
|
||||
proxy_redirect ~^http://([^:/]+)(/.+)$ http://docker.proxy/forcecacheinsecure/$1/originalwas$2;
|
||||
proxy_redirect ~^https://([^:/]+)(/.+)$ https://docker.caching.proxy.internal/forcecachesecure/$1/originalwas$2;
|
||||
proxy_redirect ~^http://([^:/]+)(/.+)$ http://docker.caching.proxy.internal/forcecacheinsecure/$1/originalwas$2;
|
||||
}
|
||||
|
||||
# handling for the redirect case explained above, with https.
|
||||
@@ -146,11 +217,6 @@ http {
|
||||
|
||||
# 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;
|
||||
|
||||
# Some debugging headers. Not important
|
||||
add_header X-Docker-Caching-Proxy-Real-Proto https;
|
||||
add_header X-Docker-Caching-Proxy-Real-Host $realHost;
|
||||
add_header X-Docker-Caching-Proxy-Real-Path $realPath;
|
||||
}
|
||||
|
||||
# handling for the redirect case explained above, with http.
|
||||
@@ -161,11 +227,6 @@ http {
|
||||
|
||||
# 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;
|
||||
|
||||
# Some debugging headers. Not important
|
||||
add_header X-Docker-Caching-Proxy-Real-Proto http;
|
||||
add_header X-Docker-Caching-Proxy-Real-Host $realHost;
|
||||
add_header X-Docker-Caching-Proxy-Real-Path $realPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user