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 。 我需要删除$host中的.another.com
Nginxstringreplace对我来说是新的
server { server_name ~^(?<subdomain>.*)\.another\.com$; root /var/www/html/$subdomain; index index.html index.htm index.php; location ~ \.php$ { <...> } }
基于这个答案 :
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; } }