Ajnin123
By:
Ajnin123

How do I set up Flask on multiple domains

February 4, 2015 3.3k views

Hi all,

I recently set up a test Flask site using this easy to use tutorial here, and I was curious how I could set this up on my droplet so that I could point different domains and subdomains to one droplet with multiple Flask projects. I have looked up other tutorials on using Apache, but the explanations of how it works and how I can implement it were often unclear. I know how I can point a domain to a Droplet IP using an A Record on my host, and linking that to the DNS settings on my control panel, but would it be possible to potentially link this to a port (in this case, multiple ports) on an ip, and then configure my Flask program to run on that port?

Thanks,

Ajnin.

2 Answers

The example that you are using serves the content directly over port 80 on the public interface:

from flask import Flask
app = Flask(__name__, static_folder='')

@app.route('/')
def index():
    return app.send_static_file('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)

If you had two Flask apps running on the same droplet, you'd want to serve them locally on different ports. Like:

    app.run(host='127.0.0.1', port=8000, debug=True)

and

    app.run(host='127.0.0.1', port=7000, debug=True)

Though using something like Gunicorn is probably your best bet. Among other considerations, you won't need to hardcode the host and port in your app.

Then you'd use a reverse proxy like Nginx to direct requests to the correct app. A simplified version of the Nginx config might look like:

upstream app_server_one {
    server 127.0.0.1:8000 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:7000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: my-domain.com

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_one;
    }
}

server {
    listen 80 default_server;
    server_name: subproject.my-domain.com

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

Hopefully this points you in the right direction! Check out this article for more info:

by O.S Tezer
In this DigitalOcean article, we'll begin with expanding our knowledge on the excellent Gunicorn WSGI HTTP Server and continue with deploying Python WSGI web applications built atop various popular frameworks.
  • Thanks so much! I'll have a go and respond if I have any issues.

  • This doesn't seem to be working as planned (still connection issues on a custom domain). I'll open a ticket and see if can get resolved.

I created a package to deploy sites in virtualenv, run scripts, and deploy workers with supervisor. And it works well on DigitalOcean, as you can easily deploy multiple flask apps on a single server.

Deployapp : https://github.com/mardix/deployapp

Have another answer? Share your knowledge.