Regular expressions in server_name: pattern length

Is it possible to capture the length of a particular pattern in the
server_name section?

For example, I’m having trouble getting the following to match:

server_name ~([a-z]{2,3})?\d{3,6}.mysite.org;

I’m using 0.7.62.

On Wed, Dec 09, 2009 at 09:21:24AM +0000, Phillip O. wrote:

Is it possible to capture the length of a particular pattern in the
server_name section?

For example, I’m having trouble getting the following to match:

server_name ~([a-z]{2,3})?\d{3,6}.mysite.org;

I’m using 0.7.62.

Do you mean how to learn the caputured data length ? No way.


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

Igor S. wrote:

I’m using 0.7.62.

Do you mean how to learn the caputured data length ? No way.

To clarify, I’m looking to be able to capture:

asd12345.mysite.org
xy789.mysite.org

but using a separate rule capture:

client1.mysite.org
anotherclient.mysite.org

for a different server block.

This is not possible then?

On Wed, Dec 09, 2009 at 09:44:01AM +0000, Phillip O. wrote:

but using a separate rule capture:

client1.mysite.org
anotherclient.mysite.org

for a different server block.

This is not possible then?

It’s possible:

server {
server_name ~^([a-z]{2,3})?\d{3,6}.mysite.org$;
}

server {
server_name ~^(\w+).mysite.org$;
}

You may also control the order of sites.


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

On Wed, Dec 09, 2009 at 10:21:11AM +0000, Phillip O. wrote:

Is this with the latest devel, or stable?

regex as a main server_name supported since 0.6.25.
server_name captures can be used since 0.7.40.
Named server_name captures can be used since 0.8.25.

You may also control the order of sites.

Can you give an example on how to do this?

server_name’s regular expressions are tested in order listed in
configuration file. You should list first more specific servers.


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

On Wed, Dec 9, 2009 at 4:49 AM, Igor S. [email protected] wrote:

Named server_name captures can be used since 0.8.25.

What do you mean by ‘named’

On Wed, Dec 09, 2009 at 04:51:33AM -0800, Michael S. wrote:

On Wed, Dec 9, 2009 at 4:49 AM, Igor S. [email protected] wrote:

Named server_name captures can be used since 0.8.25.

What do you mean by ‘named’

server {
server_name ~^(www.)?(?.+)$;

location / {
    root  /sites/$domain;
}

}


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

Igor S. wrote:

It’s possible:

server {
server_name ~^([a-z]{2,3})?\d{3,6}.mysite.org$;
}

server {
server_name ~^(\w+).mysite.org$;
}

Is this with the latest devel, or stable?

You may also control the order of sites.

Can you give an example on how to do this?

Thanks Igor!

cool!

I only use captures in one place but this might make things nicer. Is
that $domain accessable to anything inside of that server {} block?
even something inside of an include for example?

On Wed, Dec 09, 2009 at 04:55:41AM -0800, Michael S. wrote:

cool!

I only use captures in one place but this might make things nicer. Is
that $domain accessable to anything inside of that server {} block?

Yes.

even something inside of an include for example?

Yes.

š šserver_name š ~^(www.)?(?.+)$;

š šlocation / {
š š š šroot š/sites/$domain;
š š}
}


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

Igor S. wrote:

server {

Sorry, I must be doing something wrong but can’t see where:

server {
server_name ~^([a-z]{2,3})?\d{3,6}.mysite.org$;

}

nginx -t

[emerg]: directive “server_name” is not terminated by “;” in
/etc/nginx/vhosts/mysite.org:7
configuration file /etc/nginx/nginx.conf test failed

nginx -V

nginx version: nginx/0.7.62
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-44)
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid
–with-rtsig_module --with-select_module --with-poll_module
–with-http_ssl_module --with-http_stub_status_module
–with-http_gzip_static_module --with-http_dav_module
–with-http_flv_module --with-http_xslt_module
–with-http_random_index_module --with-http_image_filter_module
–http-log-path=/var/log/nginx/access.log --with-md5=/usr/lib
–with-sha1=/usr/lib --with-http_perl_module

On Wed, Dec 09, 2009 at 02:05:21PM +0000, Phillip O. wrote:

Named server_name captures can be used since 0.8.25.

Sorry, I must be doing something wrong but can’t see where:

server {
server_name ~^([a-z]{2,3})?\d{3,6}.mysite.org$;

}

nginx -t

[emerg]: directive “server_name” is not terminated by “;” in

This is caused by “{”:

  • server_name ~^([a-z]{2,3})?\d{3,6}.mysite.org$;
  • server_name “~^([a-z]{2,3})?\d{3,6}.mysite.org$”;


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

I’m trying this right now:

server {
listen 80;
server_name ^(?.+)(.mydomain.com)$;
rewrite ^ http://foo.net/script/index.php?title=$domain permanent;
}

also tried

server_name ^(?.+).mydomain.com$;

it says unknown variable $domain

it’s 0.8.24. also tried 0.8.33.

[emerg]: unknown “domain” variable

Any help? :slight_smile:

Hello!

On Thu, Feb 04, 2010 at 02:10:05PM -0800, Michael S. wrote:

server_name ^(?.+).mydomain.com$;

  • server_name ^(?.+).mydomain.com$;
  • server_name ~^(?.+).mydomain.com$;

it says unknown variable $domain

it’s 0.8.24. also tried 0.8.33.

0.8.25+

Maxim D.

i’m on 0.8.33 now

server_name ~^(?.+).domain.com$;

[emerg]: pcre_compile() failed: unrecognized character after (?< in
“^(?.+).domain.com$” at “domain>.+).tianocore.org$” in
/etc/nginx/nginx.conf

2010/2/4 Maxim D. [email protected]:

2010/2/4 Maxim D. [email protected]:

Either upgrade pcre library to 7.0 at least, or use (?P<…>)
syntax as available from pcre version 4.0.

I’m under the hassle of Redhat :confused:

I did try this instead

server_name ~^(.+).domain.com$;
rewrite ^ http://foo.com/index.php?title=$1 permanent;

$1 isn’t being populated. Of course, I guess $1 would be usually a
regex match for the rewrite line. How can I pass the match from
server_name down to rewrite?

Hello!

On Thu, Feb 04, 2010 at 02:50:11PM -0800, Michael S. wrote:

i’m on 0.8.33 now

server_name ~^(?.+).domain.com$;

[emerg]: pcre_compile() failed: unrecognized character after (?< in
“^(?.+).domain.com$” at “domain>.+).tianocore.org$” in
/etc/nginx/nginx.conf

Congratulations, level up! :slight_smile:

Either upgrade pcre library to 7.0 at least, or use (?P<…>)
syntax as available from pcre version 4.0.

BTW, you may read the same thing here:

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

Maxim D.

On Thu, Feb 4, 2010 at 3:18 PM, Maxim D. [email protected] wrote:

Uhm, pcre 4.0 was released in 2003. Â Even redhat should have it
already.

pcre-devel-6.6-2.el5_1.7
pcre-6.6-2.el5_1.7
pcre-6.6-2.el5_1.7
pcre-devel-6.6-2.el5_1.7

hmm. But for some reason it didn’t like your syntax. I did change to
the old style after $1 didn’t work and it did work, but I realized
that my browser forced lowercase and I need to be able to work some
magic. So after all of this work I’m winding up pushing it all to a
PHP wrapper so I can do some URL altering…

thanks. I -did- get it to work using the old regex syntax and named
captures, but wound up having to change it all anyway :slight_smile:

Hello!

On Thu, Feb 04, 2010 at 02:59:38PM -0800, Michael S. wrote:

2010/2/4 Maxim D. [email protected]:

Either upgrade pcre library to 7.0 at least, or use (?P<…>)
syntax as available from pcre version 4.0.

I’m under the hassle of Redhat :confused:

Uhm, pcre 4.0 was released in 2003. Even redhat should have it
already.

I did try this instead

server_name ~^(.+).domain.com$;
rewrite ^ http://foo.com/index.php?title=$1 permanent;

$1 isn’t being populated. Of course, I guess $1 would be usually a
regex match for the rewrite line. How can I pass the match from
server_name down to rewrite?

Yes, but it’s bad and error-prone way which will break as soon as
internal redirect happens. Either use named captures or don’t use
captures in server_name at all, e.g.

server {
    server_name  *.domain.com;

    if ($host ~ "^(.*)\.domain\.com$") {
        set $domain $1;
    }

    ...
}

Maxim D.

Michael S. wrote:

server_name ~^(.+).domain.com$;
rewrite ^ http://foo.com/index.php?title=$1 permanent;

Either do as Maxim suggested, with the P:

server_name ~ ^(?P.+).domain.com$;
rewrite ^ http://foo.net/script/index.php?title=$domain permanent;

or save the capture into a variable:

server_name ~^(.+).domain.com$;
set $domain $1;
rewrite ^ http://foo.net/script/index.php?title=$domain permanent;

Tobia