1

I'm trying to setup Nginx config to auto redirect any domain/subdomain pointed(CNAME) to my static subdomain(site.domain.com) to a respective directory inside /var/www. The directory will be named after the pointed domain/subdomain. I don't want to use VirtualHost or alter/restart Nginx everytime someone add new domain.

I stumble upon this answer but I still don't quite understand. What should I set as root directory?

current config :

server {
    server_name site.domain.com;
    listen 80 default_server;
    root /var/www/;
    index index.html index.htm;
}
1

1 Answer 1

1
rewrite  ^(.*)$  /$host/$1;

Add the above mentioned rewrite rule. This works as following.

xyz.foo.com/index.html will map to /var/www/xyz.foo.com/index.html

The caveat is if the domain/subdomain directory doesn't exist nginx will throw an error.

3
  • Thanks, it works! However xyz.foo.com doesn't default to xyz.foo.com/index.html, any solution for this?
    – Matt Norad
    Dec 24, 2012 at 3:14
  • You can add the index directive wiki.nginx.org/HttpIndexModule#index
    – Sameer
    Dec 24, 2012 at 15:44
  • Already did that as per my original post. It works well before I add the rewrite rule. Now it return 404 error page.
    – Matt Norad
    Dec 25, 2012 at 5:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .