Django 让用户添加自定义域

Django Let Users Add Custom Domains

我想允许用户将他们的域映射到他们在我的 Django 应用程序中的页面。因此,如果他们的页面位于:user.mydomain.com,我希望他们能够将他们的域映射到 url,这样它就变成了:usersdomain.com .

我找到了这个答案,它让我了解了如何设置子域:

If you control the nameserver for the domain and have access to the RNDC Key, you can use the post-signup view/signal to squirt out a cname to your DNS server that will resove username.yoursite.com to yoursite.com. Make sure apache is set up to recieve a wildcard virtualhost to the correct app, and then use a custom middleware to read request.META['SERVER_NAME'].lsplit('.')[0] to see what the subdomain is. You can then use this information in your views to differentiate user subdomains.

但我不知道如何让用户将他们的域指向我的子域。

我的处理方法是,首先,指示您的用户设置适当的 DNS 记录,将他们的域指向您的域(可能是 CNAME)。

然后您需要将 settings.py 中的 ALLOWED_HOSTS 设置设置为支持的通配符 (*)。需要注意的是此配置会打开一个安全漏洞,因此您需要实施自己的主机验证,recommended at the middleware level by Django。

然后,您将不得不修改用于显示用户页面的任何技术。不要匹配子域,而是匹配整个域。