title: How should I configure Nginx to proxy to a URL passed by parameter?
body: <p>I'm trying to get access to media files (images, videos) sitting behind an OAuth2 authentication.</p>
  
  <p>In order to access the resource I need to add a custom Authorization Bearer token to the request, so I can't use a simple rewrite (well, as far as I know at least).</p>
  
  <p>It cannot be done via plain HTML (say <code>img</code> or <code>video</code> tag) so I'm considering to have Nginx proxying the queries to the final server.</p>
  
  <p>Each of the media resources would be loaded via a <code>/proxy</code> path, with a <code>token</code> parameter (for authentication) and <code>url</code> for the actual resource to load.</p>
  
  <p>Sample URL:</p>
  
  <pre><code>http://myserver.com/proxy/?token=12345&amp;url=http://protectedserver.com/custompath/asset
  </code></pre>
  
  <p>This is what I came up with but I am not quite sure how to configure the <code>proxy_pass</code> directive since I need it to proxy to the <code>$url</code> variable specifically. I do not need to proxy the path (which would be empty anyway).</p>
  
  <pre><code>location /proxy/ {
    if ($arg_token ~ "^$") { return 404; }
    if ($arg_url ~ "^$") { return 404; }
    set $url $arg_url;
    proxy_set_header Authorization "Bearer $arg_token";
    set $args "";
    #proxy_pass $url;
  }
  </code></pre>
  
  <p><em>Note</em>: this will be run in a closed environment and only specific machines (kiosks with limited interaction) will be able to access the page so I'm not concerned about a potential leak of the auth token.</p>
  
  <p>I noticed a similar question on ServerFault, but no one had an answer to that:
  <a href="https://serverfault.com/questions/671991/nginx-proxy-pass-url-from-get-argument">https://serverfault.com/questions/671991/nginx-proxy-pass-url-from-get-argument</a></p>
  
  <p>I'm looking for a config setting to make it work or a viable alternative solution.</p>