Vhost support

So there doesn’t seem to be any interest in some kind of
vhost_alias module so I’d like to try and write one but I
need a little help with the nginx codebase.

server {
listen 80;
server_name _;
location / {
root /var/www/$host;
}
}

If I use the above then I get this in the logs…

2011/01/29 16:09:13 [error] 32404#0:
*1 “/var/www/domain.org/index.html” is not found
(2: No such file or directory), client: 192.168.1.2,
server: _, request: “GET / HTTP/1.1”, host: “domain.org

(yes, I know the /var/www/domain.org directory does not exist)

This is pretty good but I want to reverse the domain.org $host
variable and provide, say, a $rhost variable of “org/domain”.

I have an apache2 module that already does this so I just need
some orientation or assistance to find where in the nginx code
to do the same thing so any pointers would be appreciated.

Anyone care to offers some clues?

–markc

On 29/01/11, Igor S. wrote:

This is pretty good but I want to reverse the domain.org $host
variable and provide, say, a $rhost variable of “org/domain”.
[…]

Server names

Thanks for the reply, but how do I take into account…

www.username.domain.com

so, ie; the first one becomes com/domain and the last one becomes
com/domain/username/www ?

I have tried this but couldn’t get it to work…

server {
server_name ~^(?.)?(?.)?(?.)?(?.)?(?.);
#server_name
~^(?P.)?(?P.)?(?P.)?(?P.)?(?P.);
location / {
root /var/www/$p5/$p4/$p3/$p2/$p1;
}
}

–markc

On Sat, Jan 29, 2011 at 05:04:23PM +1000, Mark Constable wrote:

}
This is pretty good but I want to reverse the domain.org $host
variable and provide, say, a $rhost variable of “org/domain”.

I have an apache2 module that already does this so I just need
some orientation or assistance to find where in the nginx code
to do the same thing so any pointers would be appreciated.

Anyone care to offers some clues?

http://nginx.org/en/docs/http/server_names.html#regex_names


Igor S.
http://sysoev.ru/en/

On 29 Jan 2011 07h35 WET, [email protected] wrote:

www.domain.com
#~^(?P.)?(?P.)?(?P.)?(?P.)?(?P.);
location / {
root /var/www/$p5/$p4/$p3/$p2/$p1;
}
}

Try with:

server_name
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.]+).(?[^.]+);

I’m uncertain about the way the root directive value gets assigned
when there are null valued variables. Never tried anything of that sort.

— appa

On Sat, Jan 29, 2011 at 05:35:19PM +1000, Mark Constable wrote:

www.domain.com
location / {
root /var/www/$p5/$p4/$p3/$p2/$p1;
}
}

Make several servers with different regexes:

server {
server_name
~^(?[^.]+).(?[^.]+).(?[^.]+).(?[^.]+)$;
location / {
root /var/www/$p4/$p3/$p2/$p1;
}
}

server {
server_name ~^(?[^.]+).(?[^.]+).(?[^.]+)$;
location / {
root /var/www/$p3/$p2/$p1;
}
}

server {
server_name ~^(?[^.]+).(?[^.]+)$;
location / {
root /var/www/$p2/$p1/www;
}
}


Igor S.
http://sysoev.ru/en/

On 29 Jan 2011 07h58 WET, [email protected] wrote:

server_name ~^(?.)?(?.)?(?.)?(?.)?(?.);
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.]+).(?[^.]+);
Make it:

server_name
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.]+).(?[^.]+)$;

Forgot to escape the “.” in the named capturing groups.

— appa

On 29/01/11, Antnio P. P. Almeida wrote:

Make it:
server_name
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.]+).(?[^.]+)$;
Forgot to escape the “.” in the named capturing groups.

Thanks Antonio, this does indeed work. The only downside I can see is
that the system file path is something like /var/www////com/domain/,
when there are less than 5 parts, but it still seems to work okay.

server {
listen 80;
server_name
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.])[.](?[^.]*)$;

location / {
root /var/www/$p5/$p4/$p3/$p2/$p1;
index index.html index.htm index.jsx index.php;
}
}

I’d still be interested to do this without requiring a regex as all
it would take is something like this small amount of code in a module,
or as a patch to core…

char *add_path_element(char *dest, char *element)
{
*dest++ = ‘/’;
strcpy(dest,element);
return dest + strlen(element);
}

[...]
server_name = ap_get_server_name(r);
buf = apr_pstrdup(r->pool, server_name);
rvp = apr_palloc(r->pool, strlen(buf)+1);

r->canonical_filename = "";

/* Work out the reverse path */
for (p = rvp, q = buf+strlen(buf); q >= buf; q–) {
if (*q == ‘.’) {
*q = ‘\0’;
p = add_path_element(p, q+1);
}
}
p = add_path_element(p,buf);
[…]

Anyone care to suggest where is the best place to start to look for
a module or patch to add something like the above?

–markc

On Sat, Jan 29, 2011 at 10:37:02PM +1000, Mark Constable wrote:

listen 80;
server_name
~^(?[^.])[.](?[^.])[.](?[^.])[.](?[^.])[.](?[^.]*)$;

location / {
root /var/www/$p5/$p4/$p3/$p2/$p1;
index index.html index.htm index.jsx index.php;
}
}

You just need to set several servers with different regexes.


Igor S.
http://sysoev.ru/en/