2015-02-11 57 views
0

我有一個Django應用程序處理兩個(或更多)網站的部分,例如該網站的「管理」和「api」部分。我還爲網站的其他部分提供了普通的html頁面,其中不需要Django。Apache配置爲靜態網站的根和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 
... 

這個工作就請求根返回靜態網站和請求都「管理」和「api」將返回Django網站,但在Django中,我發現無法區分請求來自'/ admin'還是'/ api'。 (加上我不知道,如果有在同一WSGI 2 WSGIScriptAlias指點可能會引起一些問題。)


如果有人知道的方式來實現這一目標,而不必到我的Django應用程序分成兩個(或更多)部分將不勝感激。

回答

2

我有幾乎完全相同的問題。在阿帕奇虛擬主機配置的網站使用

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