Image for post
Image for post

How To Serve Flask Applications on Nginx withOUT uWSGI

Dhia Kennouche
Jun 2, 2019 · 2 min read

Last days I was working on a project using Flask Framework, and at the end of the project I needed to serve the application on an Nginx server. I will not talk about wether it’s a good or bad idea to use uWSGI or no. If you want to serve your Flask app with Nginx and you don’t want to do it the uWSGI way, this small tutorial is for you.

How is it going to work ?

Image for post
Image for post
Figure 1.1: How the idea is going to work seen from the outside

Prerequisites

I will assume you’re already familiar with Flask(1)and have a running Ngnix(2) server.

Step 1 — Run the Flask app

I’ve created a new Flask project. It contains only one endpoint “127.0.0.1:5000/learn/hello” that will return hello when requested.

Image for post
Image for post
Figure 1.2: an example of flask app

After running this python file, we will have our app running locally and we will be ready for the ngnix fun.c

Image for post
Image for post
Figure 1.3: Screenshot from console after running the flask app.

Step 2 — Ngnix configuration

First, We need to add our locally running flask_app address to ngnix config (found at “/etc/nginx/nginx.conf” ).

upstream learn_server{
server 127.0.0.1:5000;
}
location /flask_app {
proxy_pass http://learn_server;
proxy_http_version 1.1;
proxy_set_header Connection "";
}}

And Voila!

 yourwebsite/flask_app/learn/hello  => ngnix_server
ngnix_server => 127.0.0.1/learn/hello

In a nutshell, all requests sent to “yourwebsite/flask_app” will be forwarded to the locally running flask_app by ngnix.

Leave a comment if you think I missed something here that needs to be added or you have any questions. Happy coding ❤

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store