Stephen Malone
Stephen Malone

Reputation: 63

How can I use a single server to deploy multiple WSGI apps on multiple domains?

Hypothetical

Question / TL;DR

How can I use a server mounted at one domain to deploy two WSGI apps to two domains? Do I need to install software especially for this case, or is it just a matter of pointing the apps at my chosen domains?

Thank you for any and all advice.

Upvotes: 3

Views: 2379

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159905

Once you have DNS set up to point both app-one.com and app-two.com to myserver.com's IP address then you need to set up something to route requests coming in on port 80 (or 443 if you are going to use SSL) to each of your apps. This is normally done with virtual hosts in Apache or nginx.

If you need to run both applications in the same Python process (whether you are using a non-Python webserver as your application container or not) then you will need to dispatch to each of your apps by hand:

from werkzeug.exceptions import NotImplemented
from werkzeug.wsgi import get_host

class DomainDispatcher(object):
    """Simple domain dispatch"""
    def __init__(self, domain_handlers, default_handler=None):
        self.domain_handlers = domain_handlers
        self.default_handler = domain_handlers.get("default", default_handler)
        if self.default_handler is None:
            self.default_handler = NotImplemented()

    def __call__(self, environ, start_response):
        host = get_host(environ)
        handler = self.domain_handlers.get(host, self.default_handler)
        return handler(environ, start_response)

An example of usage:

from app_one import app as app1
from app_two import app as app2

from domain_dispatcher import DomainDispatcher

dispatcher = DomainDispatcher({'app-one.com': app1, 'app-two.com': app2})

if __name__ == '__main__':
    # Wrap dispatcher in a WSGI container
    # such as CherryPy

Upvotes: 3

Related Questions