I would setup for this:

  • user go to test.example_site1.org
  • test.example_org1.org it's a CNAME of test.example_org2.org
  • user will see the page of test.example_org2.org

On the example_org2.org server I use nginx and I have 3 django projects.

test.example_org2.org shows the third django project. http://example_org2.org shows the first django project.

The problem is that I've setup a CNAME on the example_org1.org for pointing test.example_org1.org to test.example_org2.org , but if I try to go to http://test.example_org1.org I see the first django project, which one configurated to the main domain and not to subdomain. Otherwise if I go directly to http://test.example_org2.org all it works and I see my correct project.

Why this issue?

  • might help if you show some conigs – Mike Aug 17 '11 at 12:35

CNAME are there only to say sub1.domain has the same IP than sub2.domain. But it does not tell a web server that they should serve the same content.

When you enter the domain name on your browser, it checks the IP via a DNS query, and it gets an answer saying this is a CNAME for test.example_org2.org, which points to IP Then, the browser connects to this IP, and sends a Host header with a value of test.example_org1.org (because that is what you requested). Ngnix gets this, and as it doesn't know anything about it (is not in its config) then it serves the first virtualhost it has.

You could tell the default virtualhost that if someone asks for test.example_org1.org then it should be redirected to test.example_org2.org (untested):

if ($host ~* "^test.example_org1.org$"){
  rewrite ^(.*)$ http://test.example_org2.org permanent;
| improve this answer | |
  • "If is Evil" wiki.nginx.org/IfIsEvil – Alexander Azarov Aug 17 '11 at 16:03
  • Agreed. He could use plain rewrite, but again, it was an example ;). – Torian Aug 17 '11 at 16:08
  • Unfortunately these examples are getting copied all over the Internet – Alexander Azarov Aug 17 '11 at 16:10
  • I'd bother on all over their site rather than all over the internet. Anyway, if it bothers him (or you ;)) that much, he could try rewrite ^/(django/path/to/app)(/.*)? test.example_org2.org/$1$2 permanent`, but there is no way to know what hostname is being requested (not that I know of). If someone does, enlighten us. – Torian Aug 17 '11 at 16:26
  • @Torian CNAME records are to say: "test.example_org1.org name is an alias for another domain: test.example_org2.org", they do not deal with ip addresses. An A DNS record however does and that's what he needed to do what he wanted. – AJP Nov 9 '16 at 14:46

what about adding test.example_org2.org to the server_name along with test.example_org1.org

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

| improve this answer | |
  • 1
    I had a A record with my own subdomain, but I wanted to mask the actual address with a CNAME, and I would always get the default_server served. This fixed it for me. – Housemd Apr 24 '16 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.