如何创建这种类型的子域:example.test.domain.com

Mac*_*Mac 3 rewrite nginx subdomain

我设法找到了用于创建子域的重写(我有一个指向我的服务器的通配符域;*.domain.com.

如果我去test.domain.com,它可以很好地横向于:

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

如果我这样做example.test.domain.com,它应该这样做:

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

这是目录中的另一个test目录。

这是我发现使用的重写,但是我应该如何在这实现两个目录子域?

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;
}
Run Code Online (Sandbox Code Playgroud)

Geo*_*lis 5

尝试 /var/www/domain.com/www/example.test/ 代替 /var/www/domain.com/www/test/example/

更新:实际上您要做的只是第二个虚拟主机,仅此而已。为什么不试试这个 nginx 配置呢?

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

只需为每个域/子域创建目录,即:

/var/www/domain.com
/var/www/test.domain.com
/var/www/example.test.domain.com
Run Code Online (Sandbox Code Playgroud)

来源:http : //wiki.nginx.org/VirtualHostExample

还可以从 Slicehost 中查看有关 nginx vhosts 的 HOWTO http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts