By culebrón


2016-08-16 19:15:54 8 Comments

Setting up Flask with uWSGI and Nginx is quite difficult, and even with buildout scripts it takes quite some time, and has to be recorded to instructions to be reproduced later.

If I don't plan a big load on server (it's hidden from public), 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 even Nginx, just running bare flask app on a port?

2 comments

@davidism 2016-08-16 19:28:55

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.

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.

@Stephen J. 2016-08-16 20:58:27

Presumably you already have a Flask app object and routes set up, but if you create the app like this:

import flask

app = flask.Flask(__name__)

then set up your @app.route()s, and then when you want to start the app:

import gevent

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

Then you can just run your application directly rather than having to tell gunicorn or uWSGI or anything else to run it for you.

I had a case where I wanted the utility of flask to build a web application (a REST API service) and found the inability to compose flask with other non-flask, non-web-service elements a problem. I eventually found gevent.wsgi.WSGIServer and it was just what I needed. After the call to app_server.serve_forever(), you can call app_server.stop() when your application wants to exit.

In my deployment, my application is listening on localhost: using flask and gevent, and then I have nginx reverse-proxying HTTPS requests on another port and forwarding them to my flask service on localhost.

@David Hamner 2019-01-17 21:34:56

import gevent.pywsgi worked!

@Anil Jagtap 2019-02-21 04:09:56

@Stephen J. where I need to write above logic? from where and how to call app_server.serve_forever()?

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Why does running Flask with Nginx require a WSGI wrapper?

0 Answered Questions

0 Answered Questions

Flask is not logging HTTP Requests

  • 2018-10-27 00:56:12
  • Neon Flash
  • 61 View
  • 0 Score
  • 0 Answer
  • Tags:   python flask

13 Answered Questions

[SOLVED] How to serve static files in Flask

0 Answered Questions

Is it necessary to always use Nginx with flask app in uwsgi/gunicorn

  • 2018-05-01 15:08:54
  • DevEx
  • 26 View
  • 0 Score
  • 0 Answer
  • Tags:   python flask

1 Answered Questions

[SOLVED] What are the advantages of connecting uWSGI to NGinx using the uWsgi protocol?

  • 2016-04-07 11:52:52
  • Adam Matan
  • 551 View
  • 3 Score
  • 1 Answer
  • Tags:   nginx wsgi uwsgi

1 Answered Questions

[SOLVED] "Empty reply from server" for Flask + uWSGI setup

1 Answered Questions

nginx configuration to serve multiple sites (subdomains) from same server

1 Answered Questions

[SOLVED] TypeError happens with Nginx but not with Flask Server

1 Answered Questions

Sponsored Content