如何在 www 子域上的 nginx 上托管 2 个虚拟主机?

How to host 2 virtual hosts on nginx on www subdomains?

我对这一切都是陌生的,并且已经努力尝试了 4 天,但没有取得任何大的成功。也许这只是一件小事,希望你能帮助我!到目前为止无法在这里找到答案(至少到现在为止还没有解决):

我有一个域,比如 example.com 已注册。我还添加了一个子域 test.example.com 并将其作为 CNAME 添加到 example.com。

然后我安装了 nginx 并按照 https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts 上的教程进行了设置,现在我将两个服务器都放在一个配置文件中。简而言之,它看起来像这样:

在 etc/nginx/sites-enabled/example.com

server {
    listen 80;


    root /var/www/example.com/html;
    index index.html index.htm;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ =404;
    }
}


server {
    listen 80;

    root /var/www/test.example.com/html;
    index index.html index.htm;

    server_name test.example.com www.test.example.com;

    location / {
        try_files $uri $uri/ =404;
    }
}

example.com 工作正常并在浏览器选项卡中向我显示 "welcome to example.com" 和一些文本 example.com 服务器如预期的那样 运行 (由我定义 index.html就像在此样品中)。

但是:如果我去测试。example.com 它会在选项卡 "welcome to example.com" 中显示我,尽管它应该是 "welcome to **test.**example.com" 并且没有 html 页面都给我看了。这只是一个空白页。

有人可以帮助我吗?我不知道,如果我已经得到了带有cname域的东西,或者是否有问题。

非常感谢您!:-)

我能够解决我的问题。它不在服务器上,而是我的域提供商中的错误设置。我不必转发我的域,只需将其设置在名称服务器中而不是指向 ip!