17ec681f3Smrgset $proxy_authorization '';
27ec681f3Smrg
37ec681f3Smrgset_by_lua $proxyuri '
47ec681f3Smrg        unescaped =  ngx.unescape_uri(ngx.var.arg_uri);
57ec681f3Smrg        it, err = ngx.re.match(unescaped, "(https?://)(.*@)?([^/]*)(/.*)?");
67ec681f3Smrg        if not it then
77ec681f3Smrg                -- Hack to cause nginx to return 404
87ec681f3Smrg                return "http://localhost/404"
97ec681f3Smrg        end
107ec681f3Smrg
117ec681f3Smrg        scheme = it[1];
127ec681f3Smrg        authstring = it[2];
137ec681f3Smrg        host = it[3];
147ec681f3Smrg        query = it[4];
157ec681f3Smrg
167ec681f3Smrg        if ngx.var.http_authorization and ngx.var.http_authorization ~= "" then
177ec681f3Smrg                ngx.var.proxy_authorization = ngx.var.http_authorization;
187ec681f3Smrg        elseif authstring then
197ec681f3Smrg                auth = string.sub(authstring, 0, -2);
207ec681f3Smrg                auth64 = ngx.encode_base64(auth);
217ec681f3Smrg                ngx.var.proxy_authorization = "Basic " .. auth64;
227ec681f3Smrg        end
237ec681f3Smrg
247ec681f3Smrg        -- Default to / if none is set to avoid using the request_uri query
257ec681f3Smrg        if not query then
267ec681f3Smrg                query = "/";
277ec681f3Smrg        end
287ec681f3Smrg
297ec681f3Smrg        return scheme .. host .. query;
307ec681f3Smrg';
317ec681f3Smrg
327ec681f3Smrgadd_header X-GG-Cache-Status $upstream_cache_status;
337ec681f3Smrgproxy_set_header Authorization $proxy_authorization;
347ec681f3Smrg
357ec681f3Smrgproxy_pass $proxyuri;
367ec681f3Smrg# Redirect back to ourselves on 301 replies
377ec681f3Smrgproxy_redirect ~^(.*)$ /cache/?uri=$1;
38