Forum: NGINX Regular expressions in server_name: pattern length

Posted by Phillip Oldham (Guest)
on 2009-12-19 15:25
(Received via mailing list)
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.
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 09:21:24AM +0000, Phillip Oldham 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 Sysoev
http://sysoev.ru/en/
Posted by Phillip Oldham (Guest)
on 2009-12-19 15:25
(Received via mailing list)
Igor Sysoev 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?
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 09:44:01AM +0000, Phillip Oldham 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 Sysoev
http://sysoev.ru/en/
Posted by Phillip Oldham (Guest)
on 2009-12-19 15:25
(Received via mailing list)
Igor Sysoev 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!
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 10:21:11AM +0000, Phillip Oldham 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 Sysoev
http://sysoev.ru/en/
Posted by Michael Shadle (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 9, 2009 at 4:49 AM, Igor Sysoev <igor@sysoev.ru> wrote:

> Named server_name captures can be used since 0.8.25.

What do you mean by 'named'
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 04:51:33AM -0800, Michael Shadle wrote:

> On Wed, Dec 9, 2009 at 4:49 AM, Igor Sysoev <igor@sysoev.ru> wrote:
> 
> > Named server_name captures can be used since 0.8.25.
> 
> What do you mean by 'named'

server {
    server_name   ~^(www\.)?(?<domain>.+)$;

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


--
Igor Sysoev
http://sysoev.ru/en/
Posted by Michael Shadle (Guest)
on 2009-12-19 15:25
(Received via mailing list)
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?
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 04:55:41AM -0800, Michael Shadle 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\.)?(?<domain>.+)$;
> >
> > š šlocation / {
> > š š š šroot š/sites/$domain;
> > š š}
> > }


--
Igor Sysoev
http://sysoev.ru/en/
Posted by Phillip Oldham (Guest)
on 2009-12-19 15:25
(Received via mailing list)
Igor Sysoev 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
Posted by Igor Sysoev (Guest)
on 2009-12-19 15:25
(Received via mailing list)
On Wed, Dec 09, 2009 at 02:05:21PM +0000, Phillip Oldham 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 Sysoev
http://sysoev.ru/en/
Posted by Michael Shadle (Guest)
on 2010-02-04 23:10
(Received via mailing list)
I'm trying this right now:

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

also tried

   server_name ^(?<domain>.+)\.mydomain\.com$;

it says unknown variable $domain

it's 0.8.24. also tried 0.8.33.

[emerg]: unknown "domain" variable

Any help? :)
Posted by Maxim Dounin (Guest)
on 2010-02-04 23:48
(Received via mailing list)
Hello!

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

>    server_name ^(?<domain>.+)\.mydomain\.com$;
-    server_name ^(?<domain>.+)\.mydomain\.com$;
+    server_name ~^(?<domain>.+)\.mydomain\.com$;

> 
> it says unknown variable $domain
> 
> it's 0.8.24. also tried 0.8.33.

0.8.25+

Maxim Dounin
Posted by Michael Shadle (Guest)
on 2010-02-04 23:50
(Received via mailing list)
i'm on 0.8.33 now

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

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

2010/2/4 Maxim Dounin <mdounin@mdounin.ru>:
Posted by Maxim Dounin (Guest)
on 2010-02-04 23:56
(Received via mailing list)
Hello!

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

> i'm on 0.8.33 now
> 
> server_name ~^(?<domain>.+)\.domain\.com$;
> 
> [emerg]: pcre_compile() failed: unrecognized character after (?< in
> "^(?<domain>.+)\.domain\.com$" at "domain>.+)\.tianocore\.org$" in
> /etc/nginx/nginx.conf

Congratulations, level up! :)

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 Dounin
Posted by Michael Shadle (Guest)
on 2010-02-04 23:59
(Received via mailing list)
2010/2/4 Maxim Dounin <mdounin@mdounin.ru>:

> 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 :/

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?
Posted by Maxim Dounin (Guest)
on 2010-02-05 00:18
(Received via mailing list)
Hello!

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

> 2010/2/4 Maxim Dounin <mdounin@mdounin.ru>:
> 
> > 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 :/

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 Dounin
Posted by Michael Shadle (Guest)
on 2010-02-05 00:31
(Received via mailing list)
On Thu, Feb 4, 2010 at 3:18 PM, Maxim Dounin <mdounin@mdounin.ru> 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 :)
Posted by Tobia Conforto (Guest)
on 2010-02-08 21:04
(Received via mailing list)
Michael Shadle 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>.+)\.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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.