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.
server {
    server_name *.com.another.com;
    location / {
        root   /var/www/html/$host;
        index  index.html;
    }
}

On above example, if someone make a request to www.jaja.com.another.com, nginx will look in directory /var/www/html/www.jaja.com.another.com

This is what I need: If someone make a request to www.jaja.com.another.com, I want nginx to look in directory /var/www/html/www.jaja.com

In other words, the $host is www.jaja.com.another.com. I need to remove .another.com in the $host

Nginx string replacement is new to me

share|improve this question

2 Answers 2

up vote 3 down vote accepted
server {
    server_name ~^(?<subdomain>.*)\.another\.com$;
    root /var/www/html/$subdomain;
    index index.html index.htm index.php;
    location ~ \.php$ { <...> }
}
share|improve this answer
    
It's better to leave the root directive in the server context –  kolbyjack May 16 '12 at 11:00
    
Fixed, thank you very much. –  Dmitry Verkhoturov May 16 '12 at 11:49

Based on this answer:

map $host $directory {

    default www;
    ~*^(?P<subdomain>[a-z0-9\-\.]+)\.com\.another\.com$        $subdomain;
}

server {
    server_name *.com.another.com;

    location / {
        root   /var/www/html/$directory;
        index  index.html;
    }

}
share|improve this answer

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.