skip to Main Content

I’m using Apache 2 on Linux 18.04 with Python 3.6. I have the following virtual host set up …

<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>

As you can see, this directive

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

captures all paths. However, I would like to set it up such that it captures all paths except “/coops*” and “/data*”. How would I configure my Alias such that this is possible?

2

Answers


  1. It’s better to use Django’s urls with regex to “catch” the urls. Define a url for “/coops*”, a url for “/data*”, and then a default url for anything else. If you want the web server to serve files directly from specific urls and not from Django, define them in <Directory...>.

    Configure Apache according to this documentation. For example:

    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>
    

    If you have urls which you want to run specific views, define them as urlpatterns (documentation). And then, define a default view which you define as the last urlpattern to “catch” any url which was not caught by a specific view, and allocate it to a default view. You can also redirect it to a specific view, for example the main page. Or, you can keep your website without a default view, and then any url which doesn’t match any view will return 404, which you can also customize if you want to.

    Login or Signup to reply.
  2. It would get unruly with many more exceptions, but you can use PCRE’s negative lookaheads easily here inside AliasMatch:

    AliasMatch ^/(?!coops/)(?!data/).* /var/www/html/client/build/$0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search