Regular expressions in server_name: pattern length

Maxim Dounin mdounin at mdounin.ru
Fri Feb 5 02:18:02 MSK 2010


Hello!

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

> 2010/2/4 Maxim Dounin <mdounin at 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



More information about the nginx mailing list