Is it worth it to try to optimize away Nginx regexes?
<p>In an Nginx virtual host, I have added these locations, two of which use regular expressions:</p>

<pre><code>    location ~ /-/pubsub/subscribe/(.*)$ {
      # subscribe to websocket
    }
    location ~ /-/pubsub/publish/(.*)$ {
      # websocket publish endpoint
    }
    location / {
      # reverse proxy to the application server
    }
</code></pre>

<p>But instead I can do something like this, to "hide" the regexes?</p>

<pre><code>    location /-/pubsub/ {       &lt;-- can be tested without any regex matching
      location ~ subscribe/(.*)$ { ... }
      location ~ publish/(.*)$ { ... }
    }

    location / {
      # reverse proxy
    }
</code></pre>

<p>It seems to me that this would avoid parsing any regex, for request matching <code>location /</code>, because they'll be compared with <code>location /-/pubsub/</code> (no regex) instead of <code>location ~ /-/pubsub/whatever/(.*)$</code> (with regex), right?</p>

<p>In the same way, I've separated my video uploads from other uploads, because the video uploads make use of a regex:</p>

<pre><code>  location /-/uploads/public/video/ {
    location ~ \.(mp4|m4v|m4a)$ {   &lt;-- regex matching for videos only
      mp4;
    }
  }

  location /-/uploads/public/ {
    # all other files: no regex matching needed
  }
</code></pre>

<p>But I'm not sure if this a-tiny-bit-more-complicated configuration in order to avoid regexes, makes sense. Saving videos in a different folder, just to avoid regexes. Does it ought to be faster? And is it worth the trouble?</p>