V
V
Victor Kapralov2019-09-10 16:26:59
Nginx
Victor Kapralov, 2019-09-10 16:26:59

How to dynamically create subdomains in nginx?

Hello!
It is impossible to make dynamically created subdomains for a site in any way. That is, if the user clicks on a link like: sub1.test.ru nginx should look in the F:\Server\OSPanel\domains\localhost\sub1 directory, if the user clicks on a link like othersitename.test.ru nginx should look in the F directory: \Server\OSPanel\domains\localhost\othersitename
Tried like this:

server {
    server_name ~^(www\.)?(?<domain>.+)$;

    location / {
        root F:\Server\OSPanel\domains\localhost\$domain;
    }
}

didn't work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-09-10
@dodo512

^(www\.)?(?<domain>.+)$

Here, the entire string will be captured in the $domain variable. sub1.test.ru
With this pattern, only the necessary part gets into $domain. sub1
Replacing (?:www\.|)with will (?>www\.|)exclude a match withwww.test.ru

E
Enj0y, 2019-09-10
@Enj0y

Just name folders like domain names, and there will be no extra logic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question