Store part of subdomain in a variable to use as root

Igor Sysoev is at rambler-co.ru
Fri Aug 7 12:32:42 MSD 2009


On Fri, Aug 07, 2009 at 02:27:00AM -0400, martinc6 wrote:

> martinc6 Wrote:
> -------------------------------------------------------
> > Martin Schut Wrote:
> > --------------------------------------------------
> > -----
> > > On Thu, 06 Aug 2009 21:54:56 +0200, martinc6 
> > > wrote:
> > > 
> > > > martinc6 Wrote:
> > > >
> > >
> > --------------------------------------------------
> > 
> > > -----
> > > >> Igor Sysoev Wrote:
> > > >>
> > >
> > --------------------------------------------------
> > 
> > > 
> > > >> -----
> > > >> > On Thu, Aug 06, 2009 at 02:24:53AM -0400,
> > > >> martinc6
> > > >> > wrote:
> > > >> >
> > > >> > > I need to store part of a subdomain in a
> > > >> > variable in order to use it as my root and
> > I
> > > >> can't
> > > >> > seem to get it to work.
> > > >> > >
> > > >> > > The URLs would be formed like:
> > > >> > martin_branch.dev.testsite.com
> > > >> > >
> > > >> > > And I need root to be
> > > >> >
> > /home/martin/martin_branch.dev.testsite.com
> > > >> > >
> > > >> > > The underscore ("_") and stuff
> > afterwards
> > > is
> > > >> > optional. I'll also need
> > > >> martin.dev.testsite.com
> > > >> > to have its root as
> > > >> > /home/martin/martin.dev.testsite.com.
> > > >> > >
> > > >> > > Note that the name ("martin") and home
> > > folder
> > > >> > will vary.
> > > >> > >
> > > >> > > This is what I've got so far:
> > > >> > >
> > > >> > > server {
> > > >> > >         listen   80;
> > > >> > >         server_name
> > > >> > ~^(.+)_*\.dev\.testsite\.com$;
> > > >> > >         set $myroot $1;
> > > >> > >         root /home/$myroot/$host;
> > > >> > >  }
> > > >> > >
> > > >> > > Thanks in advance!
> > > >> >
> > > >> > What is in error_log ?
> > > >> > BTW, "_" is invalid character in domain
> > > name.
> > > >> >
> > > >> > Also for this simple configuraiton you may
> > > omit
> > > >> > "set" since $1 is never
> > > >> > overwritten:
> > > >> >
> > > >> >  server {
> > > >> >          listen   80;
> > > >> >          server_name
> > > >> > ~^(.+)_*\.dev\.testsite\.com$;
> > > >> >          root /home/$1/$host;
> > > >> >  }
> > > >> >
> > > >> >
> > > >> > --
> > > >> > Igor Sysoev
> > > >> > http://sysoev.ru/en/
> > > >>
> > > >> The reason for the set is that we have other
> > > if
> > > >> statements and rewrites but they shouldn't
> > be
> > > >> conflicting because they're the same as on
> > > other
> > > >> subdomains we use. I commented it all out
> > just
> > > in
> > > >> case to test this.
> > > >>
> > > >> In there error logs I get
> > > >> /home//martin.dev.testsite.com which means
> > > that
> > > >> $myhost is empty so the regex isn't
> > returning
> > > >> anything.
> > > >>
> > > >> We're changing our format to
> > > >> branch.martin.dev.testsite.com to be more
> > > valid.
> > > >> I'm reworking the regex like so:
> > > >> ^\.*(.+)\.dev\.testsite\.com.*$
> > > >>
> > > >> But it's not done yet clearly. In my regex
> > > tester
> > > >> that returns the name I want but nginx
> > doesn't
> > > >> seem to do it.
> > > >
> > > >
> > > > The regex we're using is:
> > > \.*([^.]+)[.]dev[.]testsite[.]com.*$
> > > >
> > > > But it doesn't seem to be returning anything
> > to
> > > the server. Still get a  
> > > > 404 and logs says it's looking for
> > > /home//martin.dev.testsite.com
> > > >
> > > > Posted at Nginx Forum:  
> > > >
> > >
> > http://forum.nginx.org/read.php?2,4750,4758#msg-47
> > 
> > > 58
> > > >
> > > >
> > > >
> > > 
> > > I think you need something like
> > > ^[^.]*\.([^.]*)\.dev\.testsite\.com$ or  
> > > ^.*\.(.*)\.dev\.testsite\.com$
> > > 
> > > Best regards,
> > > 
> > > Martin
> > > 
> > > -- 
> > > Using Opera's revolutionary e-mail client:
> > > http://www.opera.com/mail/
> > 
> > 
> > That only seems to grab 'martin" if the url is
> > branch.martin.dev.testsite.com but I also need it
> > to grab "martin" if the url is just
> > martin.dev.testsite.com
> 
> 
> Figured it out. Did:
> 
> 	if ($host ~* \.*([^.]+)\.dev\.testsite\.com.*$) {
> 		set $myroot $1;
> 	}
> 	root /home/$myroot/$host;
> 
> Not the most elegant solution but pulling $1 from the server_name didn't seem to work, it always returned a blank value.

There is no difference between "server_name" and "if" from regex point
of view. The problem is some in another place. Could you show crrent 
onfiguraiton with server_name regex and what names it should catch ?


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list