1

I am building a regular django project - the difference is this:

  1. I want the django website to only "work" on a specified subdomain - for example, http://www.foo.mydomain.com

  2. I want to use an entirely different application to run on another specified subdomain - e.g. http://www.foobar.mydomain.com

How do I setup a django project, so that it only runs on a specific subdomain, and does not intercept requests to other subdomains - so other other applications can run on other subdomains on the same parent domain?

[[Note 1]]

The second application (running on the other subdomain is not a django app). In fact, it's mattermost, that I want to run on the other subdomain - so I can integrate mattermost into my website.

[[Note 2]]

I'm using nginx + gunicorn as the server

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • How about using [`ALLOWED_HOSTS`](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) ??. So for the 1st website it'll be `ALLOWED_HOSTS = ['www.foo.mydomain.com']` and for the 2nd `ALLOWED_HOSTS = ['www.foobar.mydomain.com']`. – nik_m Apr 22 '17 at 14:36
  • How about configure your server to redirect traffic depending on the subdomain? – nik_m Apr 22 '17 at 14:46
  • @nik_m That could probably work, but I wouldn't know hoe to do that (I'm using gunix +nginx). Would you know how to redirect based on subdomain? An example config file would be great – Homunculus Reticulli Apr 22 '17 at 14:51
  • There are several sources/examples out there. How about [`this`](http://stackoverflow.com/questions/9578628/redirecting-a-subdomain-with-a-regular-expression-in-nginx) one ? I'm not an nginx expert. I'm just starting to learn it, though! – nik_m Apr 22 '17 at 14:58
  • @nik_m That could potentially be useful, I'll experiment and see if that works. Thanks – Homunculus Reticulli Apr 22 '17 at 15:17

1 Answers1

0

Use a separate server block for each domain. See this document.

server {
    server_name www.foo.mydomain.com;
    ...
}
server {
    server_name www.foobar.mydomain.com;
    ...
}

Where a server_name match cannot be found, nginx will use the default server. So define a catch-all server block to prevent nginx using one of the server blocks above.

server {
    listen 80 default_server;
    deny all;
}
Richard Smith
  • 45,711
  • 6
  • 82
  • 81