Redirect subdomain to internal location

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:
http://forum.nginx.org/read.php?2,200694,200694#msg-200694

On 25 Mai 2011 12h43 WEST, [email protected] 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

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:
http://forum.nginx.org/read.php?2,200694,200739#msg-200739

On 25 Mai 2011 14h16 WEST, [email protected] 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;

(…) # put one location here
}

server {
server_name www4.tmp2app.com;

(…) # put the other location here
}

server {
server_name *.tmp2app.com;

(…) # put the remaining here
}

— appa

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:
http://nginx.org/en/docs/http/server_names.html


Igor S.

On 25 Mai 2011 15h26 WEST, [email protected] 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

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? 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:
http://forum.nginx.org/read.php?2,200694,200774#msg-200774

On 25 Mai 2011 16h07 WEST, [email protected] 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

On 25 Mai 2011 15h26 WEST, [email protected] 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

On 25 Mai 2011 19h14 WEST, [email protected] 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

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
[email protected]
http://nginx.org/mailman/listinfo/nginx

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

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
[email protected]
http://nginx.org/mailman/listinfo/nginx

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

On 26 Mai 2011 11h47 WEST, [email protected] 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 are now matched. You’re
capturing only the 1st component of the URI ‘whatever’.

— appa

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:
http://forum.nginx.org/read.php?2,200694,201110#msg-201110

Appa,

Thanks for your help and patience!

Chris

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,200694,201148#msg-201148

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs