1

Im looking to do the following match:
*-server.*.* for example - dev-server.anydomain.anytld

in the same file, another server:
*-cloud.*.* for example - dev-cloud.anydomain.anytld

fallback - in the same file - if non of the above were NOT matched:
*.*.* for example - dev.anydomain.anytld or somethingelse.anydomain.anytld

This is what I have now which is not working:

server {
 listen       80;
 server_name  ~(server)*$;
}

server {
 listen       80;
 server_name  ~(cloud)*$;
}

server {
 listen       80;
 server_name  *.*.*$;
}

you can use this tool to handle it https://nginx.viraptor.info/

Thanks

1 Answer 1

1

Your regular expressions are not valid.

To match *-server.*.* use:

server_name ~-server\.[^.]+\.[^.]+$;

To match *-cloud.*.* use:

server_name ~-cloud\.[^.]+\.[^.]+$;

You do not need a server_name statement to match anything else, just use:

listen 80 default_server;

Also, Nginx has a wildcard syntax and a regular expression syntax for server_name directives. They have different evaluation rules. The above examples use regular expressions, which are evaluated in order until a matching expression is found. See this document for details.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, the first config answers to both domains: demo-server.domain.com and demo.domain.com
It doesn't match the regex so it must be using that server block as the default_server. Have you defined a default_server?
yes. this server_name ~-server\.[^.]+\.[^.]+$; and default_server. it always goes to the server...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.