Access the second-level domain name mapped through CNAME, and nginx returns the welcome page?

this is the case. For example, aaa.com represents the primary site, and bbb.aaa.com is a sub-site. Now I have mapped ccc.com to bbb.aaa.com, through CNAME on Tencent Cloud, while accessing ccc.com does not jump to bbb.aaa.com. Nginx is used as the proxy in the background. The specific configuration is as follows

server {
    listen 8081;
    server_name *.aaa.com;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        if ($host = "aaa.com") {
            proxy_pass http://127.0.0.1:8000;
        }

        if ($host = "www.aaa.com") {
            proxy_pass http://127.0.0.1:8000;
        }

        if ($host ~ ^(\b(?!www\b)\w+).aaa.com$) {
            proxy_pass http://127.0.0.1:8080;
        }
    }
}

visiting ccc.com returns the welcome page of nginx. I don"t know much about

when I do nginx for the first time.
Mar.17,2021

  1. adding ccc.com CNAME to bbb.aaa.com,Host should be bbb.aaa.com, so just leave an adaptation rule for bbb.aaa.com
  2. it is recommended to use aaa.com CNAME to www.aaa.com, so that you can write less universal adaptive judgment logic (otherwise, it will be annoying to judge that aaa.com points to www.aaa.com by default, so you might as well give it to domain name resolution)
  3. In the
  4. Nginx configuration, avoid using if instructions as much as possible, which is an officially accepted best practice. Of course, if you want to write a Lua script or something, say
  5. look at the port 8081 given by listen in your question. I don't know what it means. The general web page defaults to port 80
  6. .
  7. the second item below is written by your original configuration. In fact, I personally prefer to hit accurately (that is, to write "bbb.aaa.com" directly instead of using wildcards)

write a set below, you try it (some of you haven't run before and don't know the effect):

 

1. I changed the configuration according to the way you wrote it. Now accessing ccc.com jumps directly to aaa.com, but direct access to bbb.aaa.com can be successfully accessed. After CNAME parsing, is it through ip to access it? because my aaa.com site and bbb.aaa.com site are on the same server.

  1. Port 8081 is mainly used for testing
  2. .
  3. the reason for using wildcards is that the number of second-level domain names that need to be resolved is not fixed, and the number will increase.
Menu