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

问题描述 投票:0回答:1

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

<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*"以外的所有路径都被捕获。 我如何配置我的Alias使之成为可能?

django python-3.x apache redirect alias
1个回答
0
投票

这样做会有更多的异常,但是你可以在AliasMatch里面很容易地使用PCRE的负值看头。

AliasMatch ^/(?!coops/)(?!data/).* /var/www/html/client/build/$0

0
投票

最好使用Django的URLs和regex来 "捕获 "这些URLs. 为 "coops*"定义一个url,为 "data*"定义一个url,然后为其他任何东西定义一个默认url。如果你想让web服务器直接从特定的url中而不是从Django中获取文件,可以在下面的命令中定义它们 <Directory...>.

根据这个配置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>

如果你想运行特定的视图,将它们定义为 urlpatterns (文件). 然后,定义一个默认视图,你将其定义为最后一个urlpattern,以 "捕获 "任何没有被特定视图捕获的url,并将其分配给一个默认视图。你也可以把它重定向到一个特定的视图,例如主页面。或者,你也可以保持你的网站没有默认视图,然后任何不匹配任何视图的url都会返回404,如果你想的话,你也可以自定义。

© www.soinside.com 2019 - 2024. All rights reserved.