问题描述
我有一台服务器,在其中托管多个域名.根据域名,我想根据该域名为相应目录提供文件.例如:
I have a server where I host multiple domain names. Depending on the domain name I'd like to serve files for a corresponding directory based on that domain name. For example:
www.domainA.com
提供/var/www/html/domainA.com
www.domainB.com
提供/var/www/html/domainB.com
等这样,我不必在每次添加站点时都编辑配置文件.在Apache中,我是这样的:
etc. This way I do not have to edit my config file every time I add a site. In Apache I did it like this:
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^(www\.)?(.*)$
RewriteRule ^(.*) /var/www/html/%2/$1
我如何在Nginx中做同样的事情?
How can I do the same thing in nginx?
推荐答案
使用服务器块外部的映射从$host
变量中捕获域名.
Capture the domain name from the $host
variable using a map outside of your server block.
map $host $domain_name {
~(?<domain>[^.]+\.[^.]+)$ $domain;
}
然后将新的$domain_name
变量添加到您的root语句中.请注意,$host
已标准化为小写,因此您的域目录也应为小写.
Then add the new $domain_name
variable to your root statement. Note that $host
is normalized to lowercase, so your domain directories should also be lowercase.
root /var/www/html/$domain_name;
确保已将服务器设置为默认服务器.
Make sure you've setup the server as the default.
这篇关于根据域名动态服务网站根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!