如何使用Nginx和uWSGI部署Flask应用?

dphi5xsq  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(151)

我需要使用Nginx和uWSGI部署一个flask应用程序并通过测试用例。
这是我的代码。

deploy.conf

server {
    listen 8000;

    location /Hello { 
        include uwsgi_params; 
        uwsgi_pass localhost:8081; 
    }
}

API.py**

from flask import Flask, request, make_response

app = Flask(__name__)
app.secret_key = "Thisisyoursecret"

@app.route('/Hello')
def hello():
    res=make_response("Welcome to your flask application")
    return res

if __name__ == "__main__":
    app.run(host='0.0.0.0')

uwsgi.ini

[uwsgi]
socket = 127.0.0.1:8081
module = wsgi:app
master = true
processes = 5

wsgi.py

from api import app
if __name__ == "__main__":
    app.run()

测试用例如下:

def test_hello_from_nginx_server(self):
    url = "http://localhost:8000/Hello"
    response = requests.get(url)
    assert response.status_code == 200
    assert response.text == 'Welcome to your flask application'

**这里是错误:assert 502 == 200

为什么我的状态码是502?

at0kjp5o

at0kjp5o1#

你能试试下面的deploy.conf是否有效吗?

server {
    listen 8000;

    location /Hello { 
        proxy_pass localhost:8081; 
    }
}

相关问题