跨子网域拆分Django项目 [英] Splitting Django project across subdomains

查看:152
本文介绍了跨子网域拆分Django项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个网站,功能需要拆分成独立的子域, dashboard.example.com admin.example。 COM facebook.example.com 。我想通过一个单一的Django项目送达一切,因为一切都将使用相同​​的核心车型。我使用Nginx的作为一个前置代理服务器处理静态文件,并通过所有其他请求到Apache。

I'm currently developing a site where the functionality needs to be split into separate subdomains, dashboard.example.com, admin.example.com, and facebook.example.com. I would like everything to be served through a single Django project because everything will be using the same core models. I'm using Nginx as a front-facing proxy server handling static files and passing all other requests to Apache.

我想到的解决办法是每个子域映射到通过nginx的适当的应用程序:

The solution I thought of was to map each of these subdomains to the appropriate app through nginx:

server {
    listen 80;
    server_name dashboard.example.com;
    ...

    location / {
        proxy_pass http://127.0.0.1/dashboard/;
        ...
    }
}

server {
    listen 80;
    server_name admin.example.com;
    ...

    location / {
        proxy_pass http://127.0.0.1/admin/;
        ...
    }
}

...这样做,为每个子域,有效地映射子域各自的应用程序的URL命名空间。我遇到的问题是,Django的不知道的映射,所以当它扭转了URL,它将prePEND /仪表板/ ,等它,网址,创建像 dashboard.example.com/dashboard/dashboard / 。我想我可以写一个自定义的逆转函数来去掉不必要的子目录,但似乎像一个创可贴。

...doing that for each subdomain, effectively mapping the subdomains to their respective app url namespaces. The problem I encountered was that Django was unaware of the mapping, so when it reversed a URL, it would prepend /dashboard/, etc. to it, creating URLs like dashboard.example.com/dashboard/dashboard/. I figure I could write a custom reverse function to strip out the unnecessary subdirectory, but that seems like a band-aid.

有没有更好的方式来完成我需要什么,或者我应该调整该项目?

Is there a better way to accomplish what I need, or should I restructure the project?

感谢您的帮助。

推荐答案

Django的网站架构(的 https://docs.djangoproject.com/en/1.7/ref/contrib/sites/ )应该足够了这一点,如果没有,看看Django的子域(的 http://django-subdomains.readthedocs.org/en/latest/ )作为似乎有一种手段解决您的反向网址(根据掀起了快速谷歌搜索,我从来没有用它自己!)

Django's Sites framework (https://docs.djangoproject.com/en/1.7/ref/contrib/sites/) should be sufficient for this, if not, take a look at django-subdomains (http://django-subdomains.readthedocs.org/en/latest/) as seems to have a means of resolving your reverse URLs (based off a quick Google search, I've never used it myself!)

这篇关于跨子网域拆分Django项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆