# Redirect subdomain to internal location

**URL:** <https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409>\
**Category:** NGINX\
**Created:** [May 25, 2011, 1:44pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409 "2011-05-25T13:44:27Z")\
**Posts on this page:** 15\
**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:** [May 25, 2011, 1:44pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/1 "2011-05-25T13:44:27Z")

</div>

How would I test for the following subdomains?

www  
www4  
any other subdomain

Then redirect each to a corresponding internal location?

I am using the following code to get the ‘any other subdomain’ but can’t  
get the redirect to a location such as @any\_subd working?

if ($host ~\* “(.\*).tm2app.com”) {  
set $subd $1;  
}

Thanks for your help.

Chris

Posted at Nginx Forum:

> **[redirect subdomain to internal location](https://forum.nginx.org/read.php?2,200694,200694#msg-200694)**
>
> How would I test for the following subdomains? www www4 any other subdomain Then redirect each to a corresponding internal location? I am using the following code to get the 'any other subdomain' but can't get the redirect to a location su

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 1:59pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/2 "2011-05-25T13:59:21Z")

</div>

On 25 Mai 2011 12h43 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if ($host ~\* “(.\*).tm2app.com”) {  
> set $subd $1;  
> }

Do you mean that _all_ subdomains should use the same location?

If so use a wildcard in the server\_name directive:

server {  
server\_name \*.tmp2app.com;

(…)  
}

If the subdomains always start by www and they’re to be handled  
differently than other do this for www1, www2…www99

server {  
server\_name “~^www\d{1,2}.tmp2app.com$”;  
(…)  
}

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 3:16pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/3 "2011-05-25T15:16:47Z")

</div>

Thanks.

No, I want www and ww4 to use different locations, with all other  
subdomains using another single location.

Thanks again,

Chris

Posted at Nginx Forum:

> **[Re: redirect subdomain to internal location](https://forum.nginx.org/read.php?2,200694,200739#msg-200739)**
>
> How would I test for the following subdomains? www www4 any other subdomain Then redirect each to a corresponding internal location? I am using the following code to get the 'any other subdomain' but can't get the redirect to a location su

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 3:24pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/4 "2011-05-25T15:24:13Z")

</div>

On 25 Mai 2011 14h16 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks.
> 
> No, I want www and ww4 to use different locations, with all other  
> subdomains using another single location.

server {  
server\_name [www.tmp2app.com](http://www.tmp2app.com);

(…) # put one location here  
}

server {  
server\_name [www4.tmp2app.com](http://www4.tmp2app.com);

(…) # put the other location here  
}

server {  
server\_name \*.tmp2app.com;

(…) # put the remaining here  
}

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/5 "2011-05-25T15:21:44Z")

</div>

On Wed, May 25, 2011 at 07:43:54AM -0400, chris.percol wrote:

> if ($host ~\* “(.\*).tm2app.com”) {  
> set $subd $1;  
> }

Could you describe using examples, but not “if/set” ?  
nginx virtual server names are described here:

> **[Server names](http://nginx.org/en/docs/http/server_names.html)**

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 5:08pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/6 "2011-05-25T17:08:11Z")

</div>

On 25 Mai 2011 15h26 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how to get the first part of the uri to be my key?  
> proxy\_pass http://app\_cluster\_1/$path;  
> proxy\_redirect off;  
> }

If I’m understanding correctly what you want to do, you’ll need a  
location with a regex that captures that URI component. E.g.:

location /(?[^/]\*)/ {  
(…)  
set\_unescape\_uri $key $client\_key;  
(…)  
}

This will match all URIs of the form /whatever/anything

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 4:27pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/7 "2011-05-25T16:27:23Z")

</div>

Thanks guys,

I get that now, in my newness to nginx I was overcomplicating things. My  
server\_name is now doing what I had hoped to achieve.

If you can bear with me for one more question, how do I get the ‘client’  
out of [domain.com/client/more/stuff.htm](http://domain.com/client/more/stuff.htm)? I want to use the ‘client’ part  
of the uri to search a redis key/value pair.

I had success with this code for query strings but couldn’t work out how  
to get the first part of the uri to be my key?

```
   location / {
   eval_escalate on;
   eval $res {
           set_unescape_uri $key $arg_key;
           redis2_query get $key;
           redis2_pass 127.0.0.1:6379;
   }
   set_by_lua $path "return

```

require(‘redis.parser’).parse\_reply(ngx.arg[1])" $res;  
proxy\_pass http://app\_cluster\_1/$path;  
proxy\_redirect off;  
}

Thanks again for all the help!

Chris

Posted at Nginx Forum:

> **[Re: redirect subdomain to internal location](https://forum.nginx.org/read.php?2,200694,200774#msg-200774)**
>
> How would I test for the following subdomains? www www4 any other subdomain Then redirect each to a corresponding internal location? I am using the following code to get the 'any other subdomain' but can't get the redirect to a location su

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 5:17pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/8 "2011-05-25T17:17:28Z")

</div>

On 25 Mai 2011 16h07 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Oops.

> If I’m understanding correctly what you want to do, you’ll need a  
> location with a regex that captures that URI component. E.g.:
> 
> location /(?[^/]\*)/ {  
> (…)

set\_unescape\_uri $key $clientkey;  
^^^^^^^^^^  
Now it’s correct.

> (…)  
> }

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 5:21pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/9 "2011-05-25T17:21:48Z")

</div>

On 25 Mai 2011 15h26 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how to get the first part of the uri to be my key?  
> proxy\_pass http://app\_cluster\_1/$path;  
> proxy\_redirect off;  
> }

If I’m understanding correctly what you want to do, you’ll need a  
location with a regex that captures that URI component. E.g.:

location ~ /(?[^/]\*)/ {  
(…)  
set\_unescape\_uri $key $clientkey;  
(…)  
}

This will match all URIs of the form /whatever/anything

Forgot the little detail of the ~ for a regex based location. Now it  
seems correct.

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 9:02pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/10 "2011-05-25T21:02:25Z")

</div>

On 25 Mai 2011 19h14 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> /clientkey/some/more.htm?
> 
> Many thanks,

That’s completely generic. It’s a named group that captures any char  
excepting ‘/’.

This implicitly creates a variable $clientkey that you can use.

Try it.  
— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 25, 2011, 8:15pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/11 "2011-05-25T20:15:26Z")

</div>

## António P. P. Almeida Wrote:

> > If you can bear with me for one more question,  
> > key?  
> > $res;  
> > proxy\_pass http://app\_cluster\_1/$path;  
> > proxy\_redirect off;  
> > }
> 
> If I’m understanding correctly what you want to  
> do, you’ll need a  
> location with a regex that captures that URI  
> component. E.g.:
> 
> location ~ /(?[^/]\*)/ {

Appa,

Thanks again!

My issue is that I don’t know the value of clientkey, it could be  
several hundred possibilities.

Is there a way to extract the unknown clientkey from  
/clientkey/some/more.htm?

Many thanks,

Chris

> seems correct.
> 
> — appa
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,200694,200846#msg-200846](http://forum.nginx.org/read.php?2,200694,200846#msg-200846)

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 26, 2011, 10:35am UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/12 "2011-05-26T10:35:22Z")

</div>

## António P. P. Almeida Wrote:

> > My issue is that I don’t know the value of  
> > captures any char  
> > excepting ‘/’.
> 
> This implicitly creates a variable $clientkey that  
> you can use.
> 
> Try it.  
> — appa

Appa,

Thanks, no joy, but get it now. Think problem may be down to my perl  
version, will try with ?P.

Chris

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,200694,201071#msg-201071](http://forum.nginx.org/read.php?2,200694,201071#msg-201071)

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 26, 2011, 1:05pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/13 "2011-05-26T13:05:40Z")

</div>

On 26 Mai 2011 11h47 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nearly there!
> 
> Thanks to Appa I have got this far…
> 
> location ~ /(?[^/]\*)/ {
> 
> What I didn’t consider was that sometimes the result I am testing  
> for may not have a proceeding ‘/’.

location ~ ^/(?[^/]\*) {  
(…)  
}

All requests of the form [domain.com/whatever](http://domain.com/whatever) are now matched. You’re  
capturing only the 1st component of the URI ‘whatever’.

— appa

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 26, 2011, 12:48pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/14 "2011-05-26T12:48:19Z")

</div>

Nearly there!

Thanks to Appa I have got this far…

location ~ /(?[^/]\*)/ {

What I didn’t consider was that sometimes the result I am testing for  
may not have a proceeding ‘/’.

How I update the rexex to allow for /mytest as well as /mytest?

Many thanks,

Chris

Posted at Nginx Forum:

> **[Re: redirect subdomain to internal location](https://forum.nginx.org/read.php?2,200694,201110#msg-201110)**
>
> How would I test for the following subdomains? www www4 any other subdomain Then redirect each to a corresponding internal location? I am using the following code to get the 'any other subdomain' but can't get the redirect to a location su

---

<div class="post-metadata">

**Author:** ![chris.percol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chris.percol](https://www.ruby-forum.com/u/chris.percol)\
**Post date:** [May 26, 2011, 2:46pm UTC](https://www.ruby-forum.com/t/redirect-subdomain-to-internal-location/207409/15 "2011-05-26T14:46:40Z")

</div>

Appa,

Thanks for your help and patience!

Chris

Posted at Nginx Forum:

> **[Re: redirect subdomain to internal location](https://forum.nginx.org/read.php?2,200694,201148#msg-201148)**
>
> How would I test for the following subdomains? www www4 any other subdomain Then redirect each to a corresponding internal location? I am using the following code to get the 'any other subdomain' but can't get the redirect to a location su
