Nginx Regex – How to Capture Regex from Server Name

nginxregex

server {
   listen (my server ip):80;
   server_name  ^(.[^\.]+)(\.)(\d+)\.vm\.example\.net$;

    location / {
        return 200 '$2';
        add_header Content-Type text/plain;
    }
}

what i am trying to do is capture any of the following domains

example1.1234.vm.example.net
example1.1234.vm.example.net
example2.4321.vm.example.net
example2.4321.vm.example.net

and i want to be able to use the number it got in the return right now its not even matching the server name and showing anything.

Best Answer

I haven't checked the validity of the regex yet, but at first glance you're missing the ~ that marks the server_name as a regular expression.

server_name  ~^(.[^\.]+)(\.)(\d+)\.vm\.example\.net$;

Documentation here

Related Topic