Search code examples
djangonginxgunicornsubdomainmattermost

nginx/gunicorn + Django: subdomain configuration for third party application integration


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


Solution

  • 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;
    }