0

I faced issue with detecting subdomain in server_name. My users may "register" their domain in my portal. They are able register only domain (user-domain.com) or/and wildcard (*.user-domain.com) or wildcard for their subdomain (*.special.user-domain.com). There is one restriction to avoid "chaining" if user-domain.com is registered they can't register whatever.user-domain.com as a domain. It's just subdomain of user-domain.com

For each domain which is not registered as wildcard I create nginx server block configuration in sites-available folder.Later I link site-eabled to correct site-available configuration. Example:

server {
    listen 443 ssl;
    server_name user-domain.com;
    ssl_certificate /etc/nginx/ssl/user-domain.com/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/user-domain.com/user-domain.com.key;
}

Certs are created via acme.sh for full domain via spining temp webserver, for wildcards via dns provider api. For wildcarded domains I use such regex, which seems to not work.

server {
    listen 443 ssl;
    server_name ~^(.*)\.wildcarded\.com$ ;
    ssl_certificate /etc/nginx/ssl/wildcarded.com/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/wildcarded.com/wildcarded.com.key
    .
    .
    .
}

Is my solution the correct way to achieve it? What should be the correct regex?

0

According to the NGINX manual on server names the following solution would be the easiest for wildcard domains, avoiding the the use of regular expression matches:

server {
   listen       443 ssl;
   server_name  *.example.org;
   ...
}

Also note, from the manual:

When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:

  1. exact name
  2. longest wildcard name starting with an asterisk, e.g. “*.example.org”
  3. longest wildcard name ending with an asterisk, e.g. “mail.*”
  4. first matching regular expression (in order of appearance in a configuration file)

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.