Django - Subdomains with Middleware or Subdomains

2019-07-09 09:09发布

问题:

I'm new to Django...

I need to start a project that will have subdomains, but I'm little confused about this subject...

I will have a website serving Spain, other serving United Kingdom and other serving United States and I will use only one domain and 3 subdomains, just like this:

Main domain:

mysite.com

Subdomains:

es.mysite.com
uk.mysite.com
us.mysite.com

These 3 different site in future could have to scale, each one in one dedicated server, but for now these 3 websites will use the same server.

In a previous question I was told to use a middleware solution(http://thingsilearned.com/2009/01/05/using-subdomains-in-django/) but I don't konw if it this that I need. With this solution if I need to scale the websites for dedicated servers each one I think will not work...

It is possible to define instead the subdomains in Apache Virtual Hosts? What do you think of using 3 different Django projects with Apache Virtual Hosts will doing the job? This is a better solution or I should go with middleware?

Best Regards,

回答1:

If you're looking to expand the subdomains to different servers eventually, the easier solution will probably be using virtual hosts on your web server.

Each host can just use a separate settings module (they can all import * from a common settings module, changing any database / language settings which are specific to that domain).

Deploying this way means you have a higher memory usage, since you will be dealing with separate instances for each domain, but that doesn't sound like it will be a problem for you and your long term direction. It also makes things simpler, using middleware can get messy (especially with tests and the like).