【问题标题】:Run Flask application with Nginx and Gunicorn使用 Nginx 和 Gunicorn 运行 Flask 应用程序
【发布时间】:2014-11-14 22:23:11
【问题描述】:

这是我第一次部署 web 应用程序,我真的需要你的帮助。 我想在使用 Nginx 和 Gunicorn 的服务器上运行 Flask Web 应用程序。我找到了this 教程,但我无法正确运行应用程序。我也尝试了在互联网上找到的其他方法,但没有。 这是我当前的 /etc/nginx/sites-available/test.conf 文件

server {
listen 80;
server_name hello.itu24.com;

root /home/ubuntu/test;

location / {
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8000;    
    }
}

这是我的应用程序 /home/ubuntu/test.py

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)

@app.route('/')
def test():
    return "Hello world!"

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == '__main__':
    app.run()

然后我运行以下命令

ubuntu@ace:~$ sudo service nginx reload
* Reloading nginx configuration nginx                           [ OK ] 
ubuntu@ace:~$ sudo gunicorn -b h 127.0.0.1:8000 test:app

然后如果我去机器的IP地址,因为我是从另一台机器连接的,我可以看到Nginx页面。但是如果我在地址 IP:8000 中添加端口,则找不到页面。我做错了什么?

【问题讨论】:

    标签: python web-applications nginx flask gunicorn


    【解决方案1】:

    Nginx 可能有另一个站点默认使用端口 80。确保您已从 /etc/nginx/sites-enabled 中删除了 default.conf 符号链接。检查 nginx.conf 以确保其中没有配置服务器。

    您的 gunicorn 命令看起来也不太正确。您需要使用应用目录中的gunicorn -b 127.0.0.1:8000 test:app

    【讨论】:

    • 太好了,现在可以了!抱歉,但我没有看到必须删除默认配置的地方。
    【解决方案2】:

    您将 gunicorn 服务器仅绑定到本地接口 127.0.0.1,因此无法从外部访问。您必须使用 nginx 作为 80 端口的反向代理。

    【讨论】:

    • 你能告诉我怎么做吗?
    • 我在配置文件中进行了这些更改。 root /home/ubuntu;proxy_pass http://SERVER_IP:8000; 它们是否正确?我以这种方式午餐应用程序sudo service nginx reload 然后gunicorn -b h 127.0.0.1:8000 test:app。最后我转到页面 SERVER_IP:8000。这样对吗?因为它仍然不起作用。请原谅我,但我是初学者。
    • 我看到的是 Nginx 的欢迎页面,而不是 webapp。 gunicorn 命令正确吗?我发现我应该使用gunicorn -b 0.0.0.0:80 test:app 来允许来自外部的连接。这个命令负责运行 webapp 吗?因为我没有运行 python test.py。
    猜你喜欢
    • 2022-01-18
    • 2022-01-15
    • 1970-01-01
    • 2012-11-19
    • 2019-03-07
    • 2016-01-19
    • 2017-01-13
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多