如何配置我的 Apache Alias 指令以捕获除两条路径之外的所有路径?

How do I configure my Apache Alias directive to capture all paths except two?

我在 Linux 18.04 和 Python 3.6 上使用 Apache 2。我设置了以下虚拟主机...

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName lab.chicommons.coop

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    LogLevel info

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias / /var/www/html/client/build/
        #<Directory /srv/rdmo/rdmo-app/static_root/>
        #    Require all granted
        #</Directory>
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

        WSGIDaemonProcess maps \
            home=/var/www/html/web python-home=/var/www/html/web/venv
        WSGIProcessGroup maps 
        WSGIScriptAlias /coops /var/www/html/web/maps/wsgi.py/coops process-group=maps
        WSGIScriptAlias /data /var/www/html/web/maps/wsgi.py/data process-group=maps
        WSGIPassAuthorization On

        <Directory /var/www/html/web/maps>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
</VirtualHost>

如你所见,这个指令

Alias / /var/www/html/client/build/

捕获所有路径。但是,我想将其设置为捕获除“/coops*”和“/data*”之外的所有路径。我将如何配置我的别名以使其成为可能?

最好将 Django 的 url 与正则表达式一起用于 "catch" url。为“/coops*”定义一个 url,为“/data*”定义一个 url,然后为其他任何东西定义一个默认的 url。如果您希望 Web 服务器直接从特定的 urls 而不是 Django 提供文件,请在 <Directory...>.

中定义它们

根据此documentation配置Apache。例如:

Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

<Directory /path/to/mysite.com/media>
Require all granted
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

如果您有 url 想要 运行 特定视图,请将它们定义为 urlpatterns (documentation)。然后,定义一个默认视图,您将其定义为最后一个 url 模式到 "catch" 任何未被特定视图捕获的 url ,并将其分配给默认视图。您还可以将其重定向到特定视图,例如主页。或者,您可以让您的网站没有默认视图,然后任何不匹配任何视图的 url 将 return 404,如果您愿意,您也可以自定义。

如果有更多异常,它会变得不守规矩,但您可以在 AliasMatch 中轻松使用 PCRE 的否定前瞻:

AliasMatch ^/(?!coops/)(?!data/).* /var/www/html/client/build/[=10=]