Use variable for server_name in nginx

I'm trying to use a variable in nginx in server_name, but is not working. Is this possible?

server {
    listen 80;
    index index.php index.html;
    set $foo "bar.example.com";
    server_name $foo;
}

Solution 1:

No it's not possible unless you want to set it to exactly the $hostname variable.

Ref:

  • http://nginx.org/en/docs/http/server_names.html

  • nginx server_name based on $hostname