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

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:


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


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;
share|improve this question

2 Answers 2

up vote 5 down vote accepted

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

  # 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

  location ~ /\.ht {
    deny  all;

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


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

share|improve this answer
That doesn't seem to work. Please bear in mind that test.domain.com will be a website also, and also example.test.domain.com will be a different website. –  MacMac Sep 29 '11 at 22:48
I have edited my answer, could you try this out too? –  George Tasioulis Sep 29 '11 at 23:02

I don't think this will work with a wildcard subdomain. You should add a new zone for test.domain.com having inside *.test.domain.com

ref: http://en.wikipedia.org/wiki/Wildcard_DNS_record

share|improve this answer
Of course if you use a control panel it may support it and do the config automatically –  jflaflamme Sep 29 '11 at 22:58
So an A type record should be added? As in, *.test.domain.com IN A <server-ip> ? –  MacMac Sep 29 '11 at 23:02
It depends of your dns software, if you manage it yourself, you should make a ZONE. A zone is a separate "file" from the one for your domain. Figure it as if you want to make a completely different domain with its on SOA, MX, etc .. but the domain name is test.domain.com. –  jflaflamme Sep 30 '11 at 0:31

Your Answer


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.