Regex on Variable ($servername)

Francis Daly francis at
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 http://localhost/
> domain is

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 like here

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
becomes and becomes

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.

Francis Daly        francis at

More information about the nginx mailing list