regexnginx

Nginx server_name - Site Regular Expression - Matching Multiple sites


I'm trying to match the following URLs with an Nginx server_name. Eg: the second part needs to match via wildcard.

websocket.etc1.africa.dating
websocket.etc2.africa.dating

I've tried regular expressions like:

server_name ~^websocket\d+\.africa\.dating$
server_name ~^websocket\..+\.africa\.dating$

I've also tried in double quotes and without.

How to match these URLs with a changing second part?


Solution

  • server_name ~^websocket\d+\.africa\.dating$ would only match URLs like (notice no . between "websocket" and "123")

    server_name ~^websocket\..+\.africa\.dating$ would only match URLs like

    What you need is probably

    server_name ~^websocket\.[a-z0-9]+\.africa\.dating$
    

    or

    server_name ~^websocket\.\w+\.africa\.dating$