我使用Apache 2.4和<Macro VHost $name $domain>
<VirtualHost *:80>
ServerName $domain
ServerAlias www.$domain
DocumentRoot "/var/www/vhosts/$name"
ErrorLog "/var/log/httpd/$name.error_log"
CustomLog "/var/log/httpd/$name.access_log" combined
</VirtualHost>
</Macro>
Use VHost example example.com
Use VHost myhost hostname.org
Use VHost apache apache.org
。根据{{3}},可以实例化几个宏,以便不重写相同的配置块。文档中的示例:
VirtualHost
我使用以下<Macro VHost $request_uri>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DBDriver mysql
DBDParams "host=localhost port=3306 user=myself pass=myselfpass dbname=apacheauth"
DBDMin 2
DBDKeep 4
DBDMax 10
DBDExptime 300
<Location $request_uri>
AuthName $request_uri
AuthType Digest
AuthDigestAlgorithm MD5
AuthDigestDomain /
AuthDigestProvider dbd
AuthDBDUserRealmQuery "SELECT MD5(password) FROM password WHERE username = %s AND realm = %s"
Require valid-user
</Location>
</VirtualHost>
</Macro>
Use VHost /test
Use VHost /anothertest
配置文件
www.mysite.com/test
问题是当我尝试访问www.mysite.com/anothertest
,询问我的凭据时,配置是否正常工作,当我尝试访问VirtualHost
时,该配置无效,向我显示当前页面而未询问凭据。似乎服务器仅为第一个Use指令实例化{{1}},跳过以下所有内容。
答案 0 :(得分:0)
这不是mod_macro的问题,而是vhosts本身。如果你手动展开它们,你会得到这个配置:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DBDriver mysql
DBDParams "host=localhost port=3306 user=myself pass=myselfpass dbname=apacheauth"
DBDMin 2
DBDKeep 4
DBDMax 10
DBDExptime 300
<Location /test>
AuthName /test
AuthType Digest
AuthDigestAlgorithm MD5
AuthDigestDomain /
AuthDigestProvider dbd
AuthDBDUserRealmQuery "SELECT MD5(password) FROM password WHERE username = %s AND realm = %s"
Require valid-user
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DBDriver mysql
DBDParams "host=localhost port=3306 user=myself pass=myselfpass dbname=apacheauth"
DBDMin 2
DBDKeep 4
DBDMax 10
DBDExptime 300
<Location /anothertest>
AuthName /anothertest
AuthType Digest
AuthDigestAlgorithm MD5
AuthDigestDomain /
AuthDigestProvider dbd
AuthDBDUserRealmQuery "SELECT MD5(password) FROM password WHERE username = %s AND realm = %s"
Require valid-user
</Location>
</VirtualHost>
您有两个vhost在同一IP地址(*
)和端口(:80
)上侦听,而没有ServerName
或ServerAlias
来区分请求。当请求进入时,Apache将加载第一个匹配的vhost,因此第二个vhost永远不会运行。
在宏中使用<VirtualHost>
只是一个例子,你可以生成你想要的任何块。看起来你真正想要的是一个用于定义受限目录的宏:
<Macro ProtectDirectory $request_uri>
<Location $request_uri>
AuthName $request_uri
AuthType Digest
AuthDigestAlgorithm MD5
AuthDigestDomain /
AuthDigestProvider dbd
AuthDBDUserRealmQuery "SELECT MD5(password) FROM password WHERE username = %s AND realm = %s"
Require valid-user
</Location>
</Macro>
然后你会有一个vhost,这个宏在它内部扩展了两次:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DBDriver mysql
DBDParams "host=localhost port=3306 user=myself pass=myselfpass dbname=apacheauth"
DBDMin 2
DBDKeep 4
DBDMax 10
DBDExptime 300
Use ProtectDirectory /test
Use ProtectDirectory /anothertest
</VirtualHost>