Deploy django app with subdomain

August 25, 2016 364 views
Django Deployment Nginx Ubuntu

I am going to deploy a django app with nginx and gunicorn. The site (example.com) has language subdomains (fr.example etc), which is managed directly by my django app (it detects the subdomains and sets the proper language). So this gona be one app for several subdomains.
So I have questions about settings:

  • What to write in Networking panel in DO?
  • How to configure Nginx?
1 Answer

The following solution will work as long as you are not hosting separate sites that also use a name.example.com subdomain on the same server and all requests for *.example.com that come to that server are expected to be served by your django app.

In the examples below should be replaced with your IP address and example.com with your domain name

What to write in Networking panel in DO?

Create the following DNS records:

@   A
*  CNAME @

The first record points your domain (example.com) to your droplet's IP address. This record handles any request for example.com without a subdomain.
The second record says that any subdomain name should point to the same address as the domain without a subdomain. In our system this wildcard should be processed after any other defined subdomain records and act as a catch all. This means that existing records for things like mail.example.com can remain and should not pose a problem.

How to configure Nginx?

If this is the only site configured in nginx and has a default_server setting attached to it's listen directive you wont have to make any changes at all since this will pass all requests on to your django application. If you have more than one site defined you'll need to make a very minor configuration change. Open your site's configuration file in /etc/nginx/sites-enabled/ and inside the server{} block you should see a server_name directive. Setting this to .example.com instead of example.com is all that is needed.

Have another answer? Share your knowledge.