在 Apache DirectoryMatch 指令中引用匹配的目录

在 Apache DirectoryMatch 指令中引用匹配的目录

我正在尝试通过 mod_wsgi 配置 Apache 以托管多个 django 站点。mod_wsgi 设置教程针对此场景提供了一个示例配置,其中每个应用程序都位于同一目录中:

WSGIScriptAliasMatch ^/([^/]+) /usr/local/django/$1/apache/django.wsgi

<DirectoryMatch ^/usr/local/django/([^/]+)/apache>
    Order deny,allow
    Allow from all
</DirectoryMatch>

我正在尝试扩展此示例以添加为每个应用程序创建的密码文件以使用 http 身份验证。我想我可以通过为每个应用程序设置一个单独的并行目录并以 WSGIScriptAliasMatch 中的方式引用匹配的目录名称来实现这一点,如下所示:

WSGIScriptAliasMatch ^/([^/]+) /usr/local/django/$1/apache/django.wsgi

<DirectoryMatch ^/usr/local/django/([^/]+)/apache>
    AuthType Basic
    AuthUserFile /usr/local/django-auth/$1/users.passwd
    AuthGroupFile /dev/null
    Require valid-user
</DirectoryMatch>

我曾假设“$1”将扩展为 DirectoryMatch 的正则表达式匹配的参数,但我无法验证并且我的错误日志显示:

No such file or directory: Could not open password file: /usr/local/django-auth/$1/users.passwd

因此,似乎“$1”并没有像我想象的那样被用在匹配的应用程序中。有什么方法可以实现这一点吗?我不想在弹出每个网站时都添加新的指令。

答案1

AuthUserFile 路径是静态的,无法根据 URL 进行扩展。

你或许应该看看:

http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms

这将允许您提供自己的身份验证提供程序。这可以查看传递给 check_password() 函数的“environ”字典中的请求信息,并根据该信息根据特定用户数据库验证用户。

答案2

请注意,对于 DirectoryMatch 指令,无论如何您都不能使用非命名的反向引用:

https://httpd.apache.org/docs/2.4/mod/core.html#directorymatch

从 2.4.8 开始,命名组和反向引用会被捕获并写入环境,其相应名称以“MATCH_”为前缀,且大写。这允许从表达式和模块(如 mod_rewrite)中引用路径元素。为了避免混淆,编号(未命名)的反向引用会被忽略。请改用命名组。

<DirectoryMatch "^/var/www/combined/(?<sitename>[^/]+)">
    Require ldap-group cn=%{env:MATCH_SITENAME},ou=combined,o=Example
</DirectoryMatch>

... 对于 2.2 来说根本不是(因为问题的标签是)

https://httpd.apache.org/docs/2.2/mod/core.html#directorymatch

指示

描述:包含适用于与正则表达式匹配的文件系统目录及其子目录的指令

Syntax:   <DirectoryMatch regex> ... </DirectoryMatch>

上下文:服务器配置,虚拟主机

状态:核心

模块:核心

<DirectoryMatch> and </DirectoryMatch> are used to enclose a group of directives which will apply only to the named directory and sub-directories of that directory (and the files within), the same as <Directory>. However, it takes as an argument a regular expression. For example:

将匹配 /www/ 中由三个数字组成的目录。

行尾字符 行尾字符 ($) 不能与此指令匹配。

也可以看看

<Directory> for a description of how regular expressions are mixed in with normal <Directory>s

How <Directory>, <Location> and <Files> sections work for an explanation of how these different sections are combined when a request is received

相关内容