加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx – 为子域问题设置CNAME

发布时间:2020-12-13 21:39:17 所属栏目:Nginx 来源:网络整理
导读:我会为此设置: 用户转到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项目

我会为此设置:

>用户转到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项目,其中一个配置为main域而不是子域.否则,如果我直接去http://test.example_org2.org它的全部工作,我看到了我正确的项目.

为什么这个问题?

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

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

您可以告诉默认的虚拟主机,如果有人要求test.example_org1.org,那么它应该被重定向到test.example_org2.org(未经测试):

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读