title: Global auth_request for nginx
body: <p>I have a server where I run multiple subdomains sites with nginx </p>
  
  <p>each subdomain have a <code>server { ... }</code> block in configuration (in <code>sites-enabled</code> )</p>
  
  <p>I want to add a <strong>global</strong> dynamic authentication backend (another http site) that will check requested subdomain and give access or ask user credentials</p>
  
  <p>so the flow should be like</p>
  
  <pre><code>request stie1.example.com ---&gt; [auth check in database] 
                                  /              \
                                no               yes
                                /                  \
              show username/pass form        pass to site1.example.com
  </code></pre>
  
  <p>I found <a href="http://nginx.org/en/docs/http/ngx_http_auth_request_module.html" rel="noreferrer">auth_request</a> module - but examples only about <code>server</code> context</p>
  
  <p>and I want it on top level context (<code>http</code> context) - where of course  <code>location</code> directive is not available</p>
  
  <p>So basically what I want is sort of global nginx middleware where I can run some code (i.e. python) to check if user have permission to view one of the hosted domains </p>
  
  <p>any suggestions ?</p>