Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
add comment

2 Answers 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;
    }
}
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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