title: Can nginx be used to front sharded, HTTP-based resources?
body: <p><strong>Info</strong></p>
  
  <p><a href="http://couchdb.apache.org/" rel="nofollow">CouchDB</a> is a RESTful, HTTP-based NoSQL datastore. Responses are sent back in simple JSON and it is capable of <a href="http://wiki.apache.org/couchdb/HTTP_Document_API#ETags.2BAC8-Caching" rel="nofollow">utilizing ETags</a> in the generated responses to help caching servers tell if data has changed or not.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Is it possible to use nginx to front a collection of CouchDB servers where each Couch server is a shard of a larger collection (and not replicas of each other) and have it determine the target shard based on a particular aspect of the query string?</p>
  
  <p>Example Queries:</p>
  
  <pre><code>http://db.mysite.com?id=1
  http://db.mysite.com?id=2
  </code></pre>
  
  <p>Shard Logic:</p>
  
  <pre><code>shard = ${id} % 2; // even/odd
  </code></pre>
  
  <p>This isn't a straight forward "load balancing" question because I would need the same requests to always end up at the same servers, but I am curious if this type of simple routing logic can be written into an nginx site configuration.</p>
  
  <p>If it can be, what makes this solution so attractive is that you can then turn on <a href="http://www.gabrielweinberg.com/blog/2011/07/nginx-json-hacks.html" rel="nofollow">nginx caching of the JSON responses</a> from the Couch servers and have the entire setup nicely packaged up and deployed in a very capable and scalable manner.</p>