search

Home  >  Q&A  >  body text

nginx server_name matches second-level domain name problem

When configuring nginx, server_name needs to configure multiple second-level domain names. Refer to this article, the original text http://bneijt.nl/blog/post/name-based-vi...

Excerpts are as follows:

server {
    server_name ~^((?<subdomain>.*)\.)?(?<domain>[^.]+)\.(?<tld>[^.]+)$;
    if ($subdomain = "") {
        set $subdomain "_";
    }
    location / {
        index index.html;
        root /srv/http/vhost/${domain}.${tld}/${subdomain};
    }
}

My configuration is slightly modified based on this, the content is as follows:

server {
    server_name  ~^((?<subdomain>.*)\.)aa\.com$;
    if ($subdomain = "") {
        set $subdomain "www";
    }
        
    location / {
        root   /var/www/${subdomain}.aa.com/public;
        index  index.html index.htm;
    }
}

Problem:
When the subdomain is empty, it cannot be converted to www. That is, if you enter www.aa.com in the browser, you can access it, but if you enter aa.com directly, It cannot be accessed. Is it a problem with the regular expression ~^((?<subdomain>.*)\.)aa\.com$ or another problem?

我想大声告诉你我想大声告诉你2744 days ago752

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 17:19:43

    Change the regular expression to:
    ~^((?<subdomain>.*).)?aa.com$
    Try it

    Note, it is not verified in the environment.

    There is also domain name access, which requires domain name resolution and configuration of A records.
    www.aa.com and aa.com require two A records.

    Best, check both places.

    reply
    0
  • Cancelreply