Apache配置为root上的静态站点,Django配置在子位置上

时间:2015-02-11 18:55:44

标签: python django apache

我有一个Django应用程序可以处理两个(或更多)网站部分,例如" admin"和#34; 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
...

只要对root的请求返回静态站点并向两个&#34; admin&#34;和&#34; api&#34;将返回Django网站,但在Django内,我发现没有办法区分请求来自&#39; / admin&#39;或来自&#39; / api&#39;。 (另外我不确定是否有两个WSGIScriptAlias指向同一个wsgi可能会导致一些问题。)


如果有人知道如何实现这一目标而无需将我的Django应用程序拆分为两个(或更多)部分,那将非常感激。

1 个答案:

答案 0 :(得分:2)

我有几乎完全相同的问题。在Apaches虚拟主机配置中使用

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

而不是

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