让 Apache 通过单个 <Location> 服务多个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/668150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Let Apache serve multiple directories through single <Location>
提问by Luke
I have an issue with serving a large amount of VirtualHosts and I was wondering if there is a more efficient way of doing things.
我在为大量 VirtualHosts 提供服务时遇到问题,我想知道是否有更有效的做事方式。
I'm currently using the Location directive to serve a large amount projects from different departments, over 300 projects from 19 departments in total. The structures is the same for every Location directive except for the directories the files are served from. What I currently have is a large file that looks something like this:
我目前正在使用 Location 指令为来自不同部门的大量项目提供服务,总共来自 19 个部门的 300 多个项目。除了提供文件的目录外,每个 Location 指令的结构都相同。我目前拥有的是一个大文件,看起来像这样:
<VirtualHost *>
ServerName www.myserver.com
<Location /departmentA/project1>
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local/departmentA/project1/passwords
Require valid-user
</Location>
<Location /departmentA/project2>
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local/departmentA/project2/passwords
Require valid-user
</Location>
<Location /departmentB/project1>
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local/departmentA/project1/passwords
Require valid-user
</Location>
</VirtualHost>
As you can see all these structures are the same except for the paths. What I would like is of course something where I can use variables for the department and project names and just maintain a single Location directive. I assume that this is also less heavy on the server and memory.
正如您所看到的,除了路径之外,所有这些结构都是相同的。我想要的当然是我可以使用部门和项目名称的变量,并且只维护一个 Location 指令。我认为这对服务器和内存的影响也较小。
<VirtualHost *>
ServerName www.myserver.com
<Location //>
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local///passwords
Require valid-user
</Location>
</VirtualHost>
采纳答案by David Z
I don't know of any way to do that specifically, as I don't think AuthUserFileaccepts a dynamic argument. It might be easiest to write a little script in your language of choice that takes a template for the <Location>block and repeats it for each directory you want to protect. Example, in Python:
我不知道有什么具体的方法可以做到这一点,因为我认为不AuthUserFile接受动态参数。用您选择的语言编写一个小脚本可能是最简单的,该脚本采用<Location>块模板并为您要保护的每个目录重复该模板。示例,在 Python 中:
#!/usr/bin/python
import sys
print '<VirtualHost *:80>'
print ' ServerName www.myserver.com'
for path in sys.stdin:
print ' <Location /%s>' % path.strip()
print ' AuthType Basic'
print ' AuthName "By Invitation Only"'
print ' AuthUserFile /usr/local/%s/passwords' % path.strip()
print ' Require valid-user'
print ' </Location>'
print '</VirtualHost>'
For each line of the form 'departmentA/project1' it reads on standard input, it prints out the corresponding <Location>section.
对于它在标准输入上读取的“departmentA/project1”形式的每一行,它打印出相应的<Location>部分。
回答by scientastic
Have you tried the <LocationMatch> directive?
您是否尝试过<LocationMatch> 指令?
It takes a regular expression instead of a path. Depending on your paths and their associated sub-options, you may be able to reduce the hundreds of directives down to a more manageable handful of them.
它需要一个正则表达式而不是路径。根据您的路径及其相关的子选项,您可以将数百条指令减少到更易于管理的少数指令。

