With the advent of new projects on the network, it sometimes seems that the task of effectively organizing user subdomains (bob.someblog.com) seems difficult for someone. In fact, this issue is solved in one minute.
It is understood that
nginx is used as the front-end http server, and the true location of the user directory is / users / bob /.
')
To proxy the domain bob.someblog.com and all its subdirectories to someblog.com/users/bob/, you can use the following nginx configuration:
# someblog.com
server {
listen 11.22.33.44:80;
server_name someblog.com * .someblog.com;
charset utf8;
location / {
# Proxy user domains to / users / $ username
if ($ host ~ * "^ (([a-z0-9 _ \ -] +) \. someblog \ .com) $") {
set $ uid $ 2;
rewrite ^ (. *) $ / users / $ uid $ 1 break;
}
...
[a-z0-9 _ \ -] - this character class is determined by the rules for registering users, namely the set of valid characters that a name can consist of.
The only negative is the inconvenience of using an address like
www.someblog.com , one of the solutions is to redirect visitors to someblog.com:
if ($ host ~ * "^ www \ .someblog \ .com $") {
rewrite ^ (. *) $ http: //someblog.com$1 last;
}
UPD: We are talking about this kind of projects, where without a light front-end of the north, such as nginx, nowhere.
UPD2: The discussion showed that I was still outdated in views and inherited the solution from the old version of nginx, where a lot of things were not there. At
http://server-tuning.info/nginx/auto-subdomains.html, you can read a slightly more modern solution. Thank
ugnich