【问题标题】:Apache config for static site on root, and Django on sub-locations根目录上的静态站点的 Apache 配置和子位置上的 Django
【发布时间】:2015-02-11 18:55:43
【问题描述】:

我有一个 Django 应用程序可以处理网站的两个(或更多)部分,例如网站的“管理员”和“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 中这样做,使用这样的 url:

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

但我想不出一种将这些静态页面的服务委托给 Apache 的好方法。 (如果我使用 url(r'^(?P&lt;path&gt;.*)$, '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 的请求返回静态站点并且对“admin”和“api”的请求将返回 Django 站点,这就是有效的,但在 Django 中,我发现无法区分请求是否来自“/admin”或来自'/api'。 (另外我不确定是否有两个 WSGIScriptAlias 指向同一个 wsgi 可能会导致一些问题。)


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

【问题讨论】:

    标签: python django apache


    【解决方案1】:

    我遇到了几乎完全相同的问题。在站点使用的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      相关资源
      最近更新 更多