Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx server_name regex

Tags:

nginx

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.

like image 746
MortalFool Avatar asked Mar 01 '17 17:03

MortalFool


3 Answers

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
like image 95
Allen Luce Avatar answered Oct 17 '22 09:10

Allen Luce


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!

like image 39
FireGnome Avatar answered Oct 17 '22 09:10

FireGnome


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.

like image 36
AWippler Avatar answered Oct 17 '22 09:10

AWippler