【问题标题】:Error in Flask Config Resulting in Site Refusing to ConnectFlask 配置错误导致站点拒绝连接
【发布时间】:2020-06-26 11:11:30
【问题描述】:

我在 Ubuntu 18.04 VPS 上的 NGINX nginx/1.14.0 上设置了 FlaskFlask 1.1.2 。 结构是-

myproject
       |--application
                    |-- app.py
                    |-- static
                             |--styles
                                    |--style.css
                    |--templates
                               |--index.html

我已经按照教程here

要跑步,我要么做-

1)export FLASK_APP=app.py 后跟flask run

2)gunicorn3 --workers=3 application:app.

当我执行 1) 时,它显示 Flask 正在运行,但浏览器(我已经尝试过其中的 2 个)显示服务器拒绝连接。 然后我检查了 NGINX 日志,但它们也显示了connect() failed: Connection refused client:myip,server: 192.0.2.0 ,request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/",host:serverip。 已通过ufw 允许端口 5000。

当我执行 2) 时,我收到错误 ModuleNotFoundError: No module named 'application'

app.py-

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

NGINX /etc/nginx/sites-enabled/default(已被禁用)是-

server {
    listen 80;
    server_name 192.0.2.0;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

192.0.2.0 不是我的服务器 ip。另外,8000 端口没有打开。

我还没有设置 WSGI 或任何东西(我有 gunicorn)。 它今天早上工作,但现在它突然开始出错。 另外,我不希望它在开发中,而是在生产环境中。

【问题讨论】:

    标签: python-3.x nginx flask ubuntu-18.04


    【解决方案1】:

    我会推荐使用 gevent

    from gevent.pywsgi import WSGIServer
    from flask import Flask, render_template
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("index.html")
    
    http_server = WSGIServer(('0.0.0.0', 8000), app)  # ((STR(IP), PORT), flask object / app)
    http_server.serve_forever()
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2018-11-22
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多