Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

If I go to, it works just fine which transverse to:


If I do, 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/$auto_subdomain) {}
if (-f /var/www/$auto_subdomain$uri) {
    rewrite ^(.*)$ /$auto_subdomain$uri;
share|improve this question
up vote 5 down vote accepted

Try /var/www/ instead of /var/www/

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 *; # Alternately: _

  root /var/www/$host;

  error_page 404 errors/404.html;
  access_log logs/;

  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:



Also checkout this HOWTO from Slicehost regarding nginx vhosts

share|improve this answer
That doesn't seem to work. Please bear in mind that will be a website also, and also 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 having inside *


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, * 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 – 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.