Search code examples
pythonnginxflaskdeploymentuwsgi

How to deploy a Flask app with Nginx and uWSGI?


I need to deploy a flask app with Nginx and uWSGI and pass the test cases.

Here is my code.

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()

Here is the test case:

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'

Here is the error: assert 502 == 200

Why am I getting status code 502?


Solution

  • Can you have a try if the following deploy.conf works?

    server {
        listen 8000;
    
        location /Hello { 
            proxy_pass localhost:8081; 
        }
    }