Nginx proxy intercept redirect and pass custom redirect to client
<p>I have a web application that wants to access files from a third party site without CORS enabled. The requests can be to an arbitrary domain with arbitrary parameters. I'm sending a request to my domain containing the target encoded as a GET parameter, i.e.</p>

<pre><code>GET https://www.example.com/proxy/?url=http%3A%2F%2Fnginx.org%2Fen%2Fdocs%2Fhttp%2Fngx_http_proxy_module.html
</code></pre>

<p>Then in Nginx I do</p>

<pre><code>location /proxy/ {
    resolver 8.8.8.8;
    set_unescape_uri $dst $arg_url;
    proxy_pass $dst;
}
</code></pre>

<p>This works for single files, but the target server sometimes returns a Location header, which I want to intercept and modify for the client to retry.</p>

<p>Basically I would like to escape $sent_http_location, append it to <a href="https://www.example.com/proxy/?url=" rel="nofollow noreferrer">https://www.example.com/proxy/?url=</a> and pass that back to the browser to retry.</p>

<p>I've tried doing</p>

<pre><code>set_escape_uri $tmp $sent_http_location;
proxy_redirect $sent_http_header /pass/?v=$tmp;
</code></pre>

<p>but this doesn't work. I've also tried saving the Location header, then ignoring the incoming header with</p>

<pre><code>proxy_hide_header
</code></pre>

<p>and replacing it with my own</p>

<pre><code>proxy_set_header
</code></pre>

<p>but ignoring causes me to lose the variable saving it.</p>

<p>How can I configure Nginx to accomplish this handling of redirects so that I could pass a encoded URL would be returned to the user when the proxied site redirects?</p>