如何在Digital Ocean上为多个域托管多个Flask应用程序?

时间:2015-02-07 10:54:09

标签: python nginx dns flask digital-ocean

使用this guide我在Python中创建了一个基本的Flask应用程序。这很好用,没有nginx我可以使用我的域连接到它。问题是,这不能扩展到一个Droplet上的多个应用程序。为此,我尝试了这个:

  • 我通过数字域中的DNS设置连接了我的域,并将我的域链接到路由到IP。我这样做是通过将我的域中的A记录指向我的IP,并将NS记录指向Digital Ocean服务器。以下是截图:
    Domain Host
    Digital Ocean DNS Host
    An example setup from the DNS

  • 我新安装了NGINX:

    sudo aptitude install nginx  
    
  • 我做了/etc/nginx/sites-enabled/default这个:

    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;
        }
    }
    
  • 我启动了我的nginx服务器:

    sudo service nginx start
    
  • 我创建了两个应用程序(注意两个应用程序是如何在不同的端口上运行的)。这两个都默认使用nginx单独运行:

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)
  • 我开了两个应用程序:

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

这样做,我从Chrome获得了错误'此网页不可用'。测试每个应用程序,它通过键入服务器IP和端口工作,但与域连接似乎不起作用。是否有人能够指出我如何使这项工作正确的方向?

谢谢,

AJ。

更新:这是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      

更新:固定!
有关详细信息,请参阅https://gist.github.com/soheilhy/8b94347ff8336d971ad0

0 个答案:

没有答案