'Set constant variable inside server block in nginx

I am creating a server block configuration in Nginx

Below is the syntax:

server {
        server_name example.com www.example.com;
        root /var/www/html/example.com/html;
        access_log /var/log/http/example.com.access;
        error_log /var/log/http/example.com.error error;

        .........
}

In the above block, you can see that I have to define a same domain name 5 different times, is there a better way to define the domain name "example.com" only once in some variable, rather repeating it? because I have to create so many blocks for different domains.

Note:

I've already tried to set domain in a variable like this

set $domain_name example.com;

server_name $domain_name www.$domain_name;
root /var/www/html/$domain_name/html;
.....

but it's not working :(

Can anyone help please!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source