How To Serve Flask Applications on Nginx withOUT uWSGI
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 ?
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.
After running this python file, we will have our app running locally and we will be ready for the ngnix fun.c
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 ❤