map $host $proxy_upstream_mapped {
    hostnames;
    helm-charts-proxy.*      'https://kubernetes-charts.storage.googleapis.com';
    gitlab-registry-proxy.*  'https://registry.gitlab.com';
    quay-registry-proxy.*    'https://quay.io';
    default                  'https://httpbin.org';
}

server {
    listen 80;
    server_name *.example.com;

    resolver 8.8.8.8;

    location / {
        proxy_pass $proxy_upstream_mapped;
        proxy_intercept_errors on;
        error_page 301 302 307 = @handle_redirect;
    }

    location @handle_redirect {
        set $saved_redirect_location '$upstream_http_location';
        proxy_pass $saved_redirect_location;
    }
}