I'm using nginx as a reverse proxy to apache/mod_wsgi and Django. Currently everything is working fine with / aliasing my wsgi file, and /media aliasing my media directory. However, I want to set it up so that /media/foo/bar also aliases my wsgi file such that /media/foo/example.txt will serve example.txt with apache, but /media/foo/bar/example.txt will be passed along to my urls.py in Django.
I've tried adding another WSGIScriptAlias to my apache.conf above my Alias for /media/, but /media/foo/bar/example.txt is still being served by apache. My apache.conf currently looks 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 at different precedence levels, you cannot create a multi level overlapping set of URLs of more than 2 levels which alternates between use of them. The solution is to use Alias/AliasMatch directives for all sub URLs, that way they are evaluated at same level of precedence. One can still use WSGIScriptAlias for root of site.
Thus try using following with directives in order such that most nested URL patterns earlier than outer URLs.
<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 is used for the most nested as we need to adjust the value of SCRIPT_NAME, ie., mount point, seen by Django application so that request still seems to be for Django instance mounted at root. If don't do that, urls.py patterns will not work as you expect for that sub URL. Use of AliasMatch and adding matched sub pattern to RHS after script path using $1 achieves that.
Although Django mounted via two different directives, calculated SCRIPT_NAME should be same for both and so same Python sub interpreter should be used. If for some reason you think your memory use is twice what you expect, ie., two instances of Django running in different sub interpreters, you can force them to run in same by uncommenting WSGIApplicationGroup directive above. This shouldn't be required though and if think you do need it, better off going to mod_wsgi mailing list and can instruct you on how to verify whether doing as it should be and what is wrong.