根据域名动态服务网站根目录 [英] Serve website root dynamically based on domain name

查看:98
本文介绍了根据域名动态服务网站根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器,在其中托管多个域名.根据域名,我想根据该域名为相应目录提供文件.例如:

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.

这篇关于根据域名动态服务网站根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆