Using nginx to rewrite url into subdomain, make it accessible through both

Basically I have a catch server block, for example *.example.com . I want both somename.com and somename.example.com work (i.e. for somename.com/whatever and somename.example.com/whatever go through the same server block and lead to the same result) so that there is no call forwarding. There may be multiple URLs that need to be mapped to different subdomains, so maybe map might be the best way? I am not sure I would appreciate any guidance on this!

EDIT: there is only one root that handles everything (and the application uses the subdomain as an important input), but it expects a subdomain, which makes the domain rewrite in such a way that there is such an important subdomain.

+5
source share
1 answer

A map is a good idea, but you may not need it. If you accept an agreement in which somename is the name of your working directory, you can use named capture as a variable root. The following regex server_name directive should match both abc.example.com and abc.com :

 server { server_name ~^(?<somename>[^\.]+)(\.example)?\.com$; root /var/www/$somename; } 
0
source

Source: https://habr.com/ru/post/1239452/


All Articles