为子域问题设置 CNAME

Fre*_*ins 6 nginx django cname-record

我会为此设置:

  • 用户去 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_org1.org 指向 test.example_org2.org ,但是如果我尝试去http://test.example_org1.org我看到第一个django项目,其中一个配置到主域而不是子域。否则,如果我直接访问http://test.example_org2.org一切正常,我会看到正确的项目。

为什么会出现这个问题?

Tor*_*ian 5

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

当您在浏览器上输入域名时,它会通过查询检查 IP DNS,并得到一个答案,说明这是 test.example_org2.org 的 CNAME,它指向 IP 1.2.3.4。然后,浏览器连接到该 IP,并发送一个Host值为 的标头test.example_org1.org(因为这是您所请求的)。Ngnix得到这个,并且因为它不知道任何关于它的信息(不在它的配置中),所以它为它拥有的第一个虚拟主机提供服务。

您可以告诉默认虚拟主机,如果有人请求,test.example_org1.org则应将其重定向到test.example_org2.org(未经测试):

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


小智 5

将 test.example_org2.org 与 test.example_org1.org 一起添加到 server_name 怎么样?

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

...
}
Run Code Online (Sandbox Code Playgroud)