server {
server_name *.com.another.com;
location / {
root /var/www/html/$host;
index index.html;
}
}
在上面的例子中,如果有人向发出请求www.jaja.com.another.com
,nginx 将在目录中查找/var/www/html/www.jaja.com.another.com
这就是我需要的:如果有人向发出请求www.jaja.com.another.com
,我希望 nginx 在目录中查找/var/www/html/www.jaja.com
换句话说,$host
是www.jaja.com.another.com
。我需要.another.com
删除$host
Nginx 字符串替换对我来说很新鲜
答案1
server {
server_name ~^(?<subdomain>.*)\.another\.com$;
root /var/www/html/$subdomain;
index index.html index.htm index.php;
location ~ \.php$ { <...> }
}
答案2
基于这个答案:
map $host $directory {
default www;
~*^(?P<subdomain>[a-z0-9\-\.]+)\.com\.another\.com$ $subdomain;
}
server {
server_name *.com.another.com;
location / {
root /var/www/html/$directory;
index index.html;
}
}