Are a WSGI server and HTTP server required to serve a Flask app?
Asked Answered
F

3

79

Setting up Flask with uWSGI and Nginx can be difficult. I tried following this DigitalOcean tutorial and still had trouble. Even with buildout scripts it takes time, and I need to write instructions to follow next time.

If I don't expect a lot of traffic, or the app is private, does it make sense to run it without uWSGI? Flask can listen to a port. Can Nginx just forward requests?

Does it make sense to not use Nginx either, just running bare Flask app on a port?

Festination answered 16/8, 2016 at 19:15 Comment(1)
I have deployed a Flask application using Gunicorn before and it works just fine. Other than using something like Nginx to speed up serving of static files I don't understand what the point is. None of the answers here really explain. Using nginx in front of your Gunicorn Flask application might be "more professional", but other than that it seems like pointless complexity. My application does not serve static files, so I do not care about having nginx to make this faster.Nina
U
97

When you "run Flask" you are actually running Werkzeug's development WSGI server, and passing your Flask app as the WSGI callable.

The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure. It does not support all the possible features of a HTTP server.

Replace the Werkzeug dev server with a production-ready WSGI server such as Gunicorn or uWSGI when moving to production, no matter where the app will be available.


The answer is similar for "should I use a web server". WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.).


Flask documents how to deploy in various ways. Many hosting providers also have documentation about deploying Python or Flask.

Unbelievable answered 16/8, 2016 at 19:28 Comment(0)
S
35

First create the app:

import flask

app = flask.Flask(__name__)

Then set up the routes, and then when you want to start the app:

import gevent.pywsgi

app_server = gevent.pywsgi.WSGIServer((host, port), app)
app_server.serve_forever()

Call this script to run the application rather than having to tell gunicorn or uWSGI to run it.

I wanted the utility of Flask to build a web application, but had trouble composing it with other elements. I eventually found that gevent.pywsgi.WSGIServer was what I needed. After the call to app_server.serve_forever(), call app_server.stop() when to exit the application.

In my deployment, my application is listening on localhost:port using Flask and gevent, and then I have Nginx reverse-proxying HTTPS requests to it.

Scarify answered 16/8, 2016 at 20:58 Comment(1)
Although the official documentation discourages using gevent directly, I found this to be easy to set-up and most useful when you need control over the threads. You can spawn additional Greenlets with gevent.Greenlet.spawn() next to the server. It looks like I can use time.sleep() for waiting without switching threads. And gevent.sleep() when waiting while switching threads.Gynarchy
G
6

You definitely need something like a production WSGI server such as Gunicorn, because the development server of Flask is meant for ease of development without much configuration for fine-tuning and optimization. Eg. Gunicorn has a variety of configurations depending on the use case you are trying to solve. But the development flask server does not have these capabilities. In addition, these development servers show their limitations as soon as you try to scale and handle more requests.

With respect to needing a reverse proxy server such as Nginx is concerned it depends on your use case. If you are deploying your application behind the latest load balancer in AWS such as an application load balancer(NOT classic load balancer), that itself will suffice for most use cases. No need to take effort into setting up NGINX if you have that option.

The purpose of a reverse proxy is to handle slow clients, meaning clients which take time to send the request. These reverse load balancers buffer the requests till the entire request is got from the clients and send them async to Gunicorn. This improves the performance of your application considerably.

Gadabout answered 31/3, 2021 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.