I am using nginx like a reverse proxy to apache/mod_wsgi and Django. Presently things are working fine with / aliasing my wsgi file, and /media aliasing my media directory. However, I would like to put it together to ensure that /media/foo/bar also aliases my wsgi file so that /media/foo/example.txt assists example.txt with apache, but /media/foo/bar/example.txt is going to be passed along to my web addresses.py in Django.

I have attempted adding another WSGIScriptAlias to my apache.conf above my Alias for /media/, but /media/foo/bar/example.txt continues to be offered by apache. My apache.conf presently appears like this:

<VirtualHost *:8080>
    #DocumentRoot /var/www/mydomain.com/public
    ServerName mydomain.com
    ErrorLog /var/www/mydomain.com/logs/apache_error_log
    CustomLog /var/www/mydomain.com/logs/apache_access_log common

    WSGIScriptAlias /media/foo/bar /var/www/mydomain.com/src/myproject/server/django.wsgi
    Alias /media/ /var/www/mydomain.com/public/media/
    <Directory /var/www/mydomain.com/public/media>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptAlias / /var/www/mydomain.com/src/myproject/server/django.wsgi

    <Directory /var/www/mydomain.com/src/myproject/server>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Because Alias and WSGIScriptAlias are in different priority levels, you can't produce a mlm overlapping group of Web addresses in excess of 2 levels which alternates between utilization of them. The answer is by using Alias/AliasMatch directives for those sub Web addresses, this way they're examined at same degree of priority. It's possible to still use WSGIScriptAlias for cause of site.

Thus use following with directives so as so that most nested URL designs sooner than outer Web addresses.

<VirtualHost *:8080>
#DocumentRoot /var/www/mydomain.com/public
ServerName mydomain.com
ErrorLog /var/www/mydomain.com/logs/apache_error_log
CustomLog /var/www/mydomain.com/logs/apache_access_log common

AliasMatch ^/(media/foo/bar/.*) /var/www/mydomain.com/src/myproject/server/django.wsgi/$1
Alias /media/ /var/www/mydomain.com/public/media/
WSGIScriptAlias / /var/www/mydomain.com/src/myproject/server/django.wsgi

<Directory /var/www/mydomain.com/src/myproject/server>
    Options ExecCGI
    AddHandler wsgi-script .wsgi
    # WSGIApplicationGroup %{GLOBAL}
    Order allow,deny
    Allow from all
</Directory>

<Directory /var/www/mydomain.com/public/media>
    Order deny,allow
    Allow from all
</Directory>
</VirtualHost>

The AliasMatch can be used which are more nested as we have to adjust the need for SCRIPT_Title, ie., mount point, seen by Django application to ensure that request still appears to become for Django instance mounted at root. If avoid that, web addresses.py designs won't act as you anticipate for the sub URL. Utilization of AliasMatch and adding matched up sub pattern to RHS after script path using $1 accomplishes that.

Although Django mounted via two different directives, calculated SCRIPT_Title ought to be same for and thus same Python sub interpreter ought to be used. If for whatever reason you believe your memory me is two times that which you expect, ie., two cases of Django running in various sub interpreters, you are able to pressure these to run in same by uncommenting WSGIApplicationGroup directive above. This should not be needed though and when think you need to do require it, best likely to mod_wsgi subscriber list and may instruct you regarding how to verify whether doing as it ought to be and what's wrong.

The fast hack is always to move mod_wsgi in front of mod_alias inside your Apache config, but that will really lead to /media being processed because of your Django application rather.

If AliasMatch supports lookahead regexps you need to have the ability to do that:

AliasMatch /media/(?!foo) …

Which should avoid this issue - nevertheless, I am enticed to recommend serving media from another hostname (media.example.org) since that will get some nice client performance benefits because of parallelism and simplifies server optimisation questions.