为子域问题设置CNAME

我会为此设置:

  • 用户去test.example_site1.org
  • test.example_org1.org它是test.example_org2.org的CNAME
  • 用户将看到test.example_org2.org的页面

在example_org2.org服务器上使用nginx,我有3个django项目。

test.example_org2.org显示第三个django项目。 http://example_org2.org展示了第一个django项目。

问题是我已经在example_org1.org上设置了一个CNAME来指向test.example_org2.org,但是如果我尝试去http://test.example_org1.org,我会看到第一个 django项目,哪一个configuration到主域而不是子域。 否则,如果我直接去http://test.example_org2.org所有的工作,我看到我的正确的项目。

为什么这个问题?

CNAME只是说sub1.domain具有相同的IP比sub2.domain 。 但它并没有告诉Web服务器他们应该提供相同的内容。

当您在浏览器上input域名时,它会通过DNS查询来检查IP,并且会得到一个答案,表示这是一个指向IP 1.2.3.4的test.example_org2.org的CNAME 。 然后,浏览器连接到这个IP,并发送一个值为test.example_org1.orgHost头(因为这是你所要求的)。 Ngnix得到这个,因为它不知道任何事情(不在它的configuration中),那么它服务于它的第一个虚拟主机。

你可以告诉默认的虚拟主机,如果有人要求test.example_org1.org那么它应该被redirect到test.example_org2.org (未经testing):

 if ($host ~* "^test.example_org1.org$"){ rewrite ^(.*)$ http://test.example_org2.org permanent; break; } 

那么把test.example_org2.org和test.example_org1.org一起添加到server_name呢?

 server { server_name test.example_org1.org test.example_org2.org; ... }