6

server_name in nginx does not match

I want to match with such FQDNs I came up with

server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net";

To Match

ucwebapi.testme.net
ucwebapi-uccore.testme.net
ucwebapi-uccore1-0.testme.net
ucwebapi-uccore999-999.testme.net

Validated with https://regex101.com/r/tAwEp9/2

Tested with

server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";

to see if ucwebapi1.testme.de server is reachable at all.

Is there any restriction im not aware of? Thank you.

3 Answers 3

7
+50

Try this:

server_name "~^(www.)?ucwebapi(-uccore)?(\d{1,3}-\d{1,3})?\.testme\.net";

It looks like there are some missing characters between your regex101 page and what ended up in your config.

I've also tuned it a bit so that it will NOT match:

ucwebapi-uccore999.testme.net
ucwebapi-uccore-.testme.net
ucwebapi-uccore-999.testme.net
Sign up to request clarification or add additional context in comments.

Comments

1

I've never seen server_name configurations with double-quotes... but I'm not shure if that solves the problem.

Some example configurations here.

Edit: Do you have a default virtual server like this:

server {
    listen 80;
    server_name _;
    return 404; # default
}

# now add your specific server
server {
    listen 80;
    server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";
    ...
}

Specific configurations will only work if you have a default configured.

@abcdn: your absolutely right, i didn't know that!

1 Comment

Quote: "A regular expression containing the characters “{” and “}” should be quoted". src: nginx.org/en/docs/http/server_names.html
1

Try:

server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net" ucwebapi1.testme.net;

Your server_name quotes encompassed the entire line. What is probably perceived as 2 separate entries, nginx interpolated as a single entry.

Comments

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.