2

I've been trying to configure Nginx to do what I currently have set up in Apache, but no luck so far.

I would like http://$1.$2.mas.example.com (where $1 and $2 is any subdomain) to point to root with a value of /home/webcontent/$1/mas/$2/html

Any suggestions how to achieve this ?

2

Here is a simple way to do it.. not how you have it laid out but its using the whole hostname as a directory.. I will look deeper into if nginx can do what you want. I know it can do this

server {
    listen       80;
    server_name  _;

    location / {
        root   /tmp/$host;
        index  index.html index.htm;
    }
}

That will serve up content if you hit http://host1.domain.com in /tmp/host1.domain.com/index.html

EDIT

Here is what you want to do

server {
    listen       80;
    server_name  _;

    if ($host ~ (.*)\.(.*)\.domain.com) {
            set $myroot /tmp/$1/mas/$2;
    }

    location / {
        root $myroot;
        index  index.html index.htm;
    }
}
1
  • This server block won't ever be used unless it's the first one specified. server_name _; is not magical. The listen directive needs to use the [default|default_server] flag for it to get requests. Alternatively if one uses > 0.8.25 you can use named captures in the server_name directive. – Martin Fjordvald Sep 16 '10 at 14:58
0

Since nginx 0.7.40 you can use regular expression server names.

So the following might work (untested):

server {
  listen 80;
  server_name ~^(\w+)\.(\w+)\.mas\.example\.com$
  root /home/webcontent/$1/mas/$2/html
}

See http://nginx.org/en/docs/http/server_names.html#regex_names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.