title: Nginx: allow access only to referrer that match location name
body: <p>Is there a way, in nginx, to allow access to a "location" only to clients with a referrer that matches the current location name?</p>
  
  <p>This is the scenario:</p>
  
  <p><a href="http://foooooo.com/bar.org/" rel="noreferrer">http://foooooo.com/bar.org/</a></p>
  
  <p><a href="http://foooooo.com/zeta.net/" rel="noreferrer">http://foooooo.com/zeta.net/</a></p>
  
  <p>etc etc</p>
  
  <p>I want the contents of the bar.org location available only if the referrer is bar.org. The same goes for zeta.net</p>
  
  <p>I know I can do this "statically", but there are a lot of those locations and I need to find a way to do this defining only one "dynamic" location.</p>
  
  <p>Sorry for my bad english.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>I've solved this way:</p>
  
  <pre><code>location ~/([a-zA-Z0-9\.\-]*)/* {
      set $match "$1::$http_referer";
      if ($match !~* ^(.+)::http[s]*://[www]*[.]*\1.*$ ) {
          return 403;
      }
  }
  </code></pre>