Ajnin123
By:
Ajnin123

How can I host multiple Flask Apps for Multiple Domains?

February 7, 2015 5.1k views

Using this guide I created a basic Flask App in Python. This worked well, and without nginx I could connect to it using my domain. The issue is, that this wasn't scaleable to multiple apps on one droplet. To do so, I tried this:

  • I connected my domain via the DNS Settings in Digital Domain, and linked my domain to to route to the IP. I did this by pointing an A record from my domain to my IP, and NS records to the Digital Ocean servers. Here are the screenshots:

    Domain Host

    Digital Ocean DNS Host

    An example setup from the DNS

  • I fresh installed NGINX:

    sudo aptitude install nginx  
    
  • I made /etc/nginx/sites-enabled/default this:

    upstream app_server_one {
        server 127.0.0.1:7000 fail_timeout=0;
    }
    
    upstream app_server_two {
        server 127.0.0.1:8000 fail_timeout=0;
    }
    
    server {
        listen 80 default_server;
        server_name: cloud.ajnin.me
    
        # [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: cloud2.ajnin.me
    
        # [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;
        }
    }
    
  • I started my nginx server:

    sudo service nginx start
    
  • I created my two apps (note how both run on separate ports). Both of these run separately with nginx on default:

app.py

    from flask import Flask
    app = Flask(__name__)

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

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

app2.py

    from flask import Flask
    app = Flask(__name__)

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

    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=8000, debug=True)
  • I started my two apps:

    sudo (nohup) python app.py &
    sudo (nohup) python app2.py &
    

Doing this, I got the Error 'This webpage is not available' from Chrome. Testing each server individually it works by typing the server ip, but running them together and connecting with the domain doesn't seem to work. Some people have suggested that I create a VirtualHost, but I'm not sure how I could implement that for this system. Would anyone be able to point me in the right direction to how I can make this work?

Thanks,

Aj.

(I would just like to appreciate the help @asb has done to help me do this. See his responses on this and this for more info)

4 comments
  • UPDATE: Just wanted to note that going to my ip:port for each server works. The domains aren't routing to these ip:port combinations though.

  • UPDATE: This is the output for netstat -plunt

    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1085/sshd       
    tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      25111/python    
    tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      10265/python    
    tcp6       0      0 :::22                   :::*                    LISTEN      1085/sshd       
    
  • @Ajnin123 could you elaborate more? I couldn't merge the gist and the digital ocean tut. Many thanks. If you had code to share that would be great

1 Answer

I suggest you put your apps into containers.
I think you have to restart the nginx server

 sudo service nginx restart

You can refer my guide Hosting Multiple Flask Apps on Ubuntu 16.04 VPS using Docker and NGINX

Have another answer? Share your knowledge.