Django 二级域名配置

2018-08-05  本文已影响548人  yuchanns

Django 二级域名配置

      在部署网站的时候我发现django的urls并不支持二级域名的配置,而在实际工作环境中,我们经常会遇到需要使用二级域名的情况。比如网站的pc版面和手机版面,域名分别是www.mysite.comm.mysite.com。虽然可以通过mysite.com/home/mysite.com/wap/这样的方式进行访问,但这并不是一个很好的解决方案。
      接着我在github上发现一个"django-hosts"插件可以为django提供这个功能,经过漫长的折腾之后总算配置成功。
github地址:https://github.com/jazzband/django-hosts
官方说明文档地址:https://django-hosts.readthedocs.io/en/latest/
下面总结一下配置步骤。

>>>原文地址

初始状态

pip install django-hosts

      关于如何部署nginx+uwsgi+django请查阅我的另一篇日志《用Nginx部署Django+uwsgi》
      以下是我部署好的可以正确访问的项目的结构树:

.
├── db.sqlite3
├── home
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── home
│   │       └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

4 directories, 15 files

      其中关键文件的配置如下:

# mysite/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', include('home.urls')),
]
# home/urls.py

from django.conf.urls import url
import views

urlpatterns = [
    url(r'^$', views.index),
]
# home/views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.

def index(request):
    return render(request, 'home/index.html', locals())
<!-- home/templates/home/index.html -->

<DOCTYPE html>
<html>
<head>
<title>home</title>
</head>
<body>
<p>here is home</p>
</body>
</html>

      nginx和uwsgi的配置则如下:

# /etc/nginx/conf.d/www.conf

server{
        listen 80;
        server_name www.mysite.com;
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:9001;
            uwsgi_param UWSGI_CHDIR /python/mysite;
            uwsgi_param UWSGI_SCRIPT mysite.wsgi;
            client_max_body_size 35m;
        }
}
# /etc/nginx/conf.d/m.conf

server{
        listen 80;
        server_name m.mysite.com;
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:9001;
            uwsgi_param UWSGI_CHDIR /python/mysite;
            uwsgi_param UWSGI_SCRIPT mysite.wsgi;
            client_max_body_size 35m;
        }
}
# /python/uwsgi/uwsgi9001.ini

[uwsgi]
socket = 127.0.0.1:9001
master = true
vhosts = true
no-site = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 512
buffer-szie = 30000
pidfile = /python/uwsgi/uwsgi9001.pid
daemonize = /python/uwsgi/uwsgi9001.log
pythonpath = /usr/local/lib/python2.7/dist-packages

      访问www.mysite.comm.mysite.com均可以成功看到页面:

C073C910-AA74-4953-B29B-3356F36F8A0F.png
73B04A50-9F33-4386-81EF-348350B58915.png

      请先确保网站可以正常访问,便于后续如果报错方便排查

配置步骤

# mysite/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #url(r'^$', include('home.urls')),
]
# mysite/hosts.py

from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'www', 'home.urls', name='www'),
)
# mysite/settings.py

# ...省略未变动内容
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
    'django_hosts',  # 注册django_hosts的app
]

MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',  # 在中间件列表开头加入django_hosts的中间件
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_hosts.middleware.HostsRequestMiddleware',  # 在中间件列表结尾加入django_hosts的中间件
]

ROOT_URLCONF = 'mysite.urls'
ROOT_HOSTCONF = 'mysite.hosts'  # 添加ROOT_HOSTCONF参数
DEFAULT_HOST = 'www'  # 添加默认的host
# ...省略未变动内容
pip install setuptools --upgrade

即可解决问题。

python manage.py startapp wap

创建wap的app(记得到mysite/settings.py里注册wap的app!)。修改views.py以及创建wap/urls.py和wap/templates/wap/index.html文件,内容如下:

# wap/urls.py

from django.conf.urls import url
import views

urlpatterns = [
    url(r'^$', views.index),
]
<!-- wap/templates/wap/index.html -->

<DOCTYPE html>
<html>
<head>
<title>wap</title>
</head>
<body>
<p>here is wap</p>
</body>
</html>
# wap/views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.

def index(request):
    return render(request, 'wap/index.html', locals())
# mysite/hosts.py

from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'www', 'home.urls', name='www'),
    host(r'm', 'wap.urls', name='m'),  # 新增的二级域名
)

总结

      通过上面的配置,我们可以知道,django-hosts这个插件的原理就是在hosts.py里通过识别二级域名将路由导向到各自app指定的的urls.py。然后我们再对各自的urls.py进行配置就完成了。

补充

      在使用了django-hosts这个插件之后,reverse不应该再使用django自带的django.shortcuts.reverse而应该使用django_hosts.resolvers.reverse。

from django_hosts.resolvers import reverse
reverse("user:login", host="www")  # host参数指明了reverse到哪个host 
上一篇下一篇

猜你喜欢

热点阅读