Nginx – How to create this type of subdomain: example.test.domain.com

nginxrewritesubdomain

I've managed to find a rewrite for making subdomains (which I have a wildcard domain pointed to my server; *.domain.com.

If I go to test.domain.com, it works just fine which transverse to:

/var/www/domain.com/www/test/

If I do example.test.domain.com, it should do this:

/var/www/domain.com/www/test/example/

Which is another directory in the test directory.

This is the rewrite I found to be used, but how should I implement the two directory subdomain in this?

if ($host !~* ^www\.domain\.domain$) {}
if ($host ~* ^([^.]+)\.domain\.com$) {
    set $auto_subdomain $1;
}
if (-d /var/www/domain.com/www/$auto_subdomain) {}
if (-f /var/www/domain.com/www/$auto_subdomain$uri) {
    rewrite ^(.*)$ /$auto_subdomain$uri;
    break;
}

Best Answer

Try /var/www/domain.com/www/example.test/ instead of /var/www/domain.com/www/test/example/

Update: Actually what you're trying to do is just a second vhost, nothing more. Why don't you try this nginx configuration?

server {
  # Replace this port with the right one for your requirements
  listen 80 [default|default_server];  #could also be 1.2.3.4:80

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name domain.com test.domain.com example.test.domain.com *.domain.com; # Alternately: _

  root /var/www/$host;

  error_page 404 errors/404.html;
  access_log logs/star.yourdomain.com.access.log;

  index index.php index.html index.htm;

  # serve static files directly
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
  }

  location ~ /\.ht {
    deny  all;
  }
}

And just create the directories for each domain/subdomain ie:

/var/www/domain.com
/var/www/test.domain.com
/var/www/example.test.domain.com

Source: http://wiki.nginx.org/VirtualHostExample

Also checkout this HOWTO from Slicehost regarding nginx vhosts http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts