# Select upstream by subdomain

**URL:** <https://www.ruby-forum.com/t/select-upstream-by-subdomain/209611>\
**Category:** NGINX\
**Created:** [July 18, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/select-upstream-by-subdomain/209611 "2011-07-18T20:43:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 18, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/select-upstream-by-subdomain/209611/1 "2011-07-18T20:43:20Z")

</div>

We are rewriting our app and would like to migrate accounts to the new  
back end in chunks, though not in alphabetical sequence. All accounts  
have unique subdomains. We have thousands of accounts so I don’t think  
that putting the rewrite rules in manually would be feasible. I thought  
that maybe a key-value store would be a good solution, so I built nginx  
with the memc-nginx-module. Now I’m getting stuck.

Here’s what I would like to happen: when a request comes in, ngnix uses  
the subdomain as the key and checks for its existence in memcached. If  
so, it gets passed to the new back end. If not, it goes to the old back  
end.

Here’s what I have so far:

upstream memback {  
server 127.0.0.1:11211;  
keepalive 1024 single;  
}

location @old {  
proxy\_pass localhost:7001;  
}

location @new {  
proxy\_pass other\_host:7001;  
}

location / {  
set $memc\_key $arg\_key;  
memc\_pass memback;  
}

I can’t figure out how to capture the response (404 or 200) from  
memcached and route the request to the proper back end. Am I going about  
this totally wrong? Thanks!

Posted at Nginx Forum:

> **[Select upstream by subdomain](https://forum.nginx.org/read.php?2,212573,212573#msg-212573)**
>
> We are rewriting our app and would like to migrate accounts to the new back end in chunks, though not in alphabetical sequence. All accounts have unique subdomains. We have thousands of accounts so I don't think that putting the rewrite rules in...
