Is there a variable containing the domain name in nginx?

I am doing come configuration refactoring of an nginx/1.14.1 server which acts as a reverse-proxy to PHP-FPM.

The way it's now configured, I'm able to do

# in a server {} section at a vhost there's
# set $base_domain domain.com;
# and then it includes another config file which has something like this: 
if ($php_logs_path = '') {
    set $php_logs_path /a/b/$base_domain/logs/$server_name.php_www_errors.log;
}
fastcgi_parma PHP_ADMIN_VALUE "error_log=$php_logs_path"

This works fine and the log file appears at the right path:

/a/b/domain.com/logs/some-sub-domain.domain.com.php_www_errors.log

However, I don't particularly like the fact I now need to go over hundred vhosts to set the $base_domain variable. I went over the nginx docs, but I couldn't find a variable which gives only the domain segment without the sub-domain.

Is there an easy way to do this with a custom variable?


What about using some global map directive like

map $server_name $base_domain {
    ~([^.]*)\.([^.]*)$    $1.$2;
}

map blocks are defined in http context so you can define it separately from your vhosts in the global nginx config.

Update

As OP noted, this solution wouldn't work with .co.uk domain. I thought of other examples writing this answer - for example, Australian .com.au, .net.au, Ukrainian .com.ua, .in.ua and many others. Well, since the national TLDs are always two letter, we can assume that any .xx.xx and .xxx.xx domain suffix is very likely some national two component TLD. Since the list of map block expressions checked from first to last, those domains could be catched with the following map block:

map $server_name $base_domain {
    "~([^.]*)\.([a-z]{2,3})\.([a-z]{2})$"    $1.$2.$3;
    ~([^.]*)\.([^.]*)$                       $1.$2;
}

Update 2

Just figured out, there is no need for several capture groups in these regexes:

map $server_name $base_domain {
    "~([^.]*\.[a-z]{2,3}\.[a-z]{2})$"    $1;
    ~([^.]*\.[^.]*)$                     $1;
}

Why don't you just reuse $server_name? This variable is set to the first server_name defined in the server block. Just make sure the first defined name is the one you want (and it probably already is).