title: Nginx rules for upper/lower case locations
body: <p>So, here’s my situation.  I have a site that has some custom logic coming from a separate upstream application.  I have this working right now with no problems, though my straight forward solution is a little verbose.  I will soon be adding more of these custom locations, as well as adding the ability to access these locations in uppercase as well as lower.  My solution will work, but the list of locations will soon become unwieldy and I’d like to re-think my solution.</p>
  
  <p>So, let’s assume my upstream is called “myupstream” and is defined already in the file.  My custom locations will be one, two, three, and four. (real locations names changed for their own protection).</p>
  
  <p>So these urls:</p>
  
  <p><a href="http://mysite/one" rel="nofollow">http://mysite/one</a>
  <a href="http://mysite/two" rel="nofollow">http://mysite/two</a>
  <a href="http://mysite/three" rel="nofollow">http://mysite/three</a>
  <a href="http://mysite/four" rel="nofollow">http://mysite/four</a></p>
  
  <p>And in my nginx.conf:</p>
  
  <pre><code>    location /one {
              log_request_speed_filter on;
              log_request_speed_filter_timeout 1000;
              proxy_pass   http://myupstream/one;
              proxy_set_header Host $http_host;
      }
      location /two {
              log_request_speed_filter on;
              log_request_speed_filter_timeout 1000;
              proxy_pass   http://myupstream/two;
              proxy_set_header Host $http_host;
      }
  …
  etc.
  </code></pre>
  
  <p>All of the locations are identical besides the name of the location.  </p>
  
  <p>Now, the number of the locations is a few right now, but may grow to be a few dozen.  And then, add the ability to visit the locations in uppercase as well as lower case.  So:</p>
  
  <p><a href="http://mysite/one" rel="nofollow">http://mysite/one</a>
  <a href="http://mysite/ONE" rel="nofollow">http://mysite/ONE</a>
  <a href="http://mysite/two" rel="nofollow">http://mysite/two</a>
  <a href="http://mysite/TWO" rel="nofollow">http://mysite/TWO</a>
  etc…</p>
  
  <p>If I have 20 locations, using my current strategy I’d be creating 40 different location blocks inside my nginx.conf.  So, I was looking for a way that was a little more dynamic.  Some thing like:</p>
  
  <pre><code>    location /(one|two|three) {
              log_request_speed_filter on;
              log_request_speed_filter_timeout 1000;
              proxy_pass   http://myupstream/$1;
              proxy_set_header Host $http_host;
      }
  </code></pre>
  
  <p>OF course, I’d like it to ONLY work for specific entries, so a wide open rule would not work.  Plus, I’d like to detect uppercase as well, but when calling the upstream it would need to be lowercase.  For example:</p>
  
  <pre><code>    location /ONE {
              log_request_speed_filter on;
              log_request_speed_filter_timeout 1000;
              proxy_pass   http://myupstream/one;
              proxy_set_header Host $http_host;
      }
  </code></pre>
  
  <p>So question 1:</p>
  
  <p>Is there a better way to do this in nginx?  How?</p>
  
  <p>Question 2:</p>
  
  <p>Which way is more resource/process efficient?</p>
  
  <p>Thank you in advance..</p>