Regex on Variable ($servername)

Francis Daly francis at daoine.org
Fri Nov 10 14:04:01 UTC 2017


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 at daoine.org


More information about the nginx mailing list