Welcome! Log In Create A New Profile

Advanced

Regex on Variable ($servername)

Posted by basti 
basti
Regex on Variable ($servername)
October 29, 2017 12:00PM
Hello,

i try to setup a catch all proxy server with nginx.
I want to catch domains like this but have only domainname (without
subdomain) in $domain

In this example from nginx docs domain has "fullname".

server {
    server_name ~^(www\.)?(*?<domain>*.+)$;
    root /sites/*$domain*;
}

servername: www.example.com -> $domain should be example.com

Best Regards
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Francis Daly
Re: Regex on Variable ($servername)
November 10, 2017 09:30AM
On Sun, Oct 29, 2017 at 11:53:23AM +0100, basti wrote:

Hi there,

> In this example from nginx docs domain has "fullname".
>
> server {
>     server_name ~^(www\.)?(*?<domain>*.+)$;
>     root /sites/*$domain*;
> }

When I use the config

server {
server_name ~^(www\.)?(?<domain>.+)$;
return 200 "domain is $domain\n";
}

I get the message

nginx: [emerg] pcre_compile() failed: unrecognized character
after (?< in "^(www\.)?(?<domain>.+)$" at "domain>.+)$" in
/usr/local/nginx/conf/nginx.conf

because my PCRE version does not recognise that syntax.

The page at http://nginx.org/en/docs/http/server_names.html does say:

"""
The PCRE library supports named captures using the following syntax:

?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0
?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0
?P<name> Python compatible syntax, supported since PCRE-4.0
If nginx fails to start and displays the error message:
pcre_compile() failed: unrecognized character after (?< in ...
this means that the PCRE library is old and the syntax “?P<name>” should be tried instead.
"""

When I change the main line (by adding an extra P) to be

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

then it all seems to work for me:

$ curl -H Host:www.example.com http://localhost/
domain is example.com
$ curl -H Host:example.com http://localhost/
domain is example.com
$ curl -H Host:no.example.com http://localhost/
domain is no.example.com

> servername: www.example.com -> $domain should be example.com

It works for me. What output do you get instead of what you want to get?

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
basti
Re: Regex on Variable ($servername)
November 10, 2017 12:10PM
Hello,

the Problem are "multiple" subs.

When I use your example i get

curl -H Host:foo.www.example.com http://localhost/
domain is foo.www.example.com

But I need example.com like here https://regex101.com/r/9h3D77/1

Best Regards

On 10.11.2017 09:19, Francis Daly wrote:
> On Sun, Oct 29, 2017 at 11:53:23AM +0100, basti wrote:
>
> Hi there,
>
>> In this example from nginx docs domain has "fullname".
>>
>> server {
>>     server_name ~^(www\.)?(*?<domain>*.+)$;
>>     root /sites/*$domain*;
>> }
>
> When I use the config
>
> server {
> server_name ~^(www\.)?(?<domain>.+)$;
> return 200 "domain is $domain\n";
> }
>
> I get the message
>
> nginx: [emerg] pcre_compile() failed: unrecognized character
> after (?< in "^(www\.)?(?<domain>.+)$" at "domain>.+)$" in
> /usr/local/nginx/conf/nginx.conf
>
> because my PCRE version does not recognise that syntax.
>
> The page at http://nginx.org/en/docs/http/server_names.html does say:
>
> """
> The PCRE library supports named captures using the following syntax:
>
> ?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0
> ?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0
> ?P<name> Python compatible syntax, supported since PCRE-4.0
> If nginx fails to start and displays the error message:
> pcre_compile() failed: unrecognized character after (?< in ...
> this means that the PCRE library is old and the syntax “?P<name>” should be tried instead.
> """
>
> When I change the main line (by adding an extra P) to be
>
> server_name ~^(www\.)?(?P<domain>.+)$;
>
> then it all seems to work for me:
>
> $ curl -H Host:www.example.com http://localhost/
> domain is example.com
> $ curl -H Host:example.com http://localhost/
> domain is example.com
> $ curl -H Host:no.example.com http://localhost/
> domain is no.example.com
>
>> servername: www.example.com -> $domain should be example.com
>
> It works for me. What output do you get instead of what you want to get?
>
> f
>
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Francis Daly
Re: Regex on Variable ($servername)
November 10, 2017 03:10PM
On Fri, Nov 10, 2017 at 12:02:28PM +0100, basti wrote:

Hi there,

> the Problem are "multiple" subs.
>
> When I use your example i get
>
> curl -H Host:foo.www.example.com http://localhost/
> domain is foo.www.example.com

Yes - the example config you gave very specifically ignores "www." at
the start of the Host: header, and sets $domain to the rest.

If you want a different part of the Host header, you must decide what
exactly you want, and write a config that does that.

> But I need example.com like here https://regex101.com/r/9h3D77/1

I think that that says you want the capturing regex (\w+\.\w+)$ so that
you will keep the last two dot-separated parts, such that www.example.com
becomes example.com and www.example.co.uk becomes co.uk

Without testing, I'd guess that

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

probably comes close to what you want, assuming that your regex engine
inside your nginx supports \w.

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login