根目录下静态站点的 Apache 配置,子位置上的 Django

Apache config for static site on root, and Django on sub-locations

我有一个 Django 应用程序可以处理网站的两个(或更多)部分,例如网站的 "admin" 和 "api" 部分。我也有网站其余部分的正常 html 页面,不需要 Django。

我想在我网站的根目录下放置静态 html 站点,在某些子位置上放置 Django 应用程序,例如

www.example.com        -> Apache static html site
www.example.com/admin/ -> Django app, serving the pages for the admin
www.example.com/api/   -> Django app, serving the pages for the api

起初我尝试使用 Django 的所有形式,使用如下网址:

# my_app/urls.py
...
url(r'^admin/', include('admin.urls')),
url(r'^api/', include('api.urls')),
url(r'^(?P<path>.*)$', ??????????),
...

但我想不出将这些静态页面的服务委托给 Apache 的好方法。 (如果我使用 url(r'^(?P<path>.*)$, 'django.views.static.serve', {'document_root': '/path/to/static/site'}),它确实有效,但 Django 文档禁止在生产服务器上使用它。)


然后我尝试使用 apache 配置:

# httpd.conf
...
DocumentRoot /path/to/static/site
WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api   /path/to/django/my_app/wsgi.py
...

就请求根 returned 静态站点和请求 "admin" 和 "api" 将 return Django 站点而言,这是有效的,但在 Django 中我发现无法区分请求是来自“/admin”还是来自“/api”。 (另外我不确定有两个 WSGIScriptAlias 指向同一个 wsgi 是否会导致一些问题。)


如果有人知道无需将我的 Django 应用程序分成两个(或更多)部分即可实现此目的的方法,我们将不胜感激。

我遇到了几乎完全相同的问题。在站点使用的 Apache 虚拟主机配置中

WSGIScriptAliasMatch ^(/(admin|api)) /path/to/django/my_app/wsgi.py

而不是

WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api   /path/to/django/my_app/wsgi.py

查看这些问答以获取更多信息:

Django (wsgi) and Wordpress coexisting in Apache virtualhost

Reconfiguring Apache to serve website root from new php source and specific sub-urls from old django site