Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to make nginx accept websites in this format: dev.*.domain.com. I've read the docs and I understand that I have to use a regex for that.

A wildcard name may contain an asterisk only on the name's start or end, and only on a dot border. The names “www..example.org” and “w.example.org” are invalid. However, these names can be specified using regular expressions, for example, “~^www..+.example.org$” and “~^w..example.org$”. An asterisk can match several name parts. The name “.example.org” matches not only www.example.org but www.sub.example.org as well.

So I have:

server {
        listen 80;
        server_name ~^dev\..+\.domain\.com\.br$ dev.domain.com.br;

        access_log /var/log/nginx/dev.domain.com.br.access.log;
        error_log /var/log/nginx/dev.domain.com.br.error.log;

        location / {
            proxy_pass http://127.0.0.1:68319/;
            include /etc/nginx/proxy.conf;
        }
        location /media/ {
            alias /my/folder/to/media/;
            expires 15d;
        }
        location /favicon.ico { 
            alias /my/folder/to/favicon.ico;
            expires 15d;
        }
    }

Somehow it ends being catched by this rule (which is the last one):

server {
        listen 80;
        server_name *.domain.com.br domain.com.br;

        access_log /var/log/nginx/domain.com.br.access.log;
        error_log /var/log/nginx/domain.com.br.error.log;

        location / {
            proxy_pass http://127.0.0.1:8080/;
            include /etc/nginx/proxy.conf;
        }
        location /media/ {
            alias /my/path/to/media/;
            expires 15d;
        }
        location /favicon.ico { 
            alias /my/path/to/favicon.ico;
            expires 15d;
        }
    }

Any ideas? Many thanks in advance.

PS: I think it's being catched by *.domain.com because of the order nginx tests for matches. Is there anyway I can rewrite *.domain.com to only test for 1 subdomain level? i.e. test.domain.com matches *.domain.com, but sub.test.domain.com doesn't.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

*.domain.com is just a simple suffix match, there's no way to limit it to a single subdomain level. To accomplish what you want, you'll have to convert your suffix check to also be a regex:

server {
  server_name ~^dev\..+\.domain\.com\.br$ dev.domain.com.br;
  ...
}

server {
  server_name ~^[^.]+\.domain\.com\.br$ domain.com.br;
  ...
}

with both checks being regex, the order of the server blocks matters.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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