<p>I want to block access to all URLs in my server except URLs starting with <code>/myapp/path</code>, excluding <code>/myapp/path/nope</code>, which should be blocked as well.</p>
<p>I tried:</p>
<pre><code>nginx.org/server-snippets: |
  location = /myapp/path/nope { return 404; }
  location ^~ /myapp/path {}
  location / { return 404; }
</code></pre>
<p>But got 404 messages on URLs starting with <code>/myapp/path</code> as well. To be frank, even after reading the documentation and trying all sorts of things it seems I haven't figured out how nginx determines what location to serve. What it wrong with my snippet? Thanks!</p>

towel
<p>Eventually I resolved this issue using a negative regex.
In my question I used the location and the regex incorrectly, as I never told nginx what to do with the path I wrote.</p>
<p>So in order to restrict access to anything that doesn't start with <code>/myapp/path</code> use:</p>
<pre><code>location ~ ^/(?!myapp/path) { return 404; } # Or deny all;
</code></pre>
