I want to use gunicorn for a REST API application with Flask/Python. What is the purpose of adding nginx here to gunicorn? The gunicorn site recommends using gunicorn with nginx.
-
5I have the same question. The answers seem to be throwing around terms like "front-facing" and "reverse-proxy" and such. These are terms I understand just fine. My interest (whether it is inline with the OP or not...) is what, exactly, is it that Gunicorn cannot do or do well that Nginx can? Is it just serving static files efficiently? – John Carrell Dec 28 '18 at 15:47
-
4Gunicorn is an "application server" for a python program while nginx is quite an optimized web server (with many features and quite complex configuration options) which is completely what gunicorn is not designed to do. Serving static files efficiently is one. One thing which gunicorn cannot do that I have personally experienced is its poor handling of socketio forwarding. That was why I needed to use nginx in the first place. Well for more comparison you better look at gunicorn's and nginx's features list. – eddys Dec 31 '18 at 08:08
5 Answers
Nginx has some web server functionality (e.g., serving static pages; SSL handling) that gunicorn does not, whereas gunicorn implements WSGI (which nginx does not).
... Wait, why do we need two servers? Think of Gunicorn as the application web server that will be running behind nginx – the front- facing web server. Gunicorn is WSGI-compatible. It can talk to other applications that support WSGI, like Flask or Django.
Source: https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/

- 25,981
- 23
- 80
- 125

- 1,147
- 1
- 8
- 14
-
8NGINX and Apache can't directly communicate with Python base web applications that's why we need of gateway interface to interpret and handle request to Python. We have different gateway's Mod_WSGI, uWSGI and Gunicorn. First request received by Apache or NGINX then gateway's to python application and vice versa. – Muhammad Faizan Fareed Jun 12 '20 at 16:24
Nginx is a reverse proxy for Gunicorn. Gunicorn serves your Flask app and Nginx sits in front of it and decides where a request should go. For example, if the incoming request is an http request Nginx redirects it to gunicorn, if it is for a static file, it serves it itself. Read more about how to use Nginx and Gunicorn and how to deploy them starting from here.

- 3,807
- 3
- 33
- 50

- 13,083
- 10
- 47
- 91
Gunicorn is an application server for running your python application instance.
NGINX is a reverse proxy. It accepts incoming connections and decides where they should go next. It is in front of Gunicorn.

- 508
- 5
- 15
Do you know why the Django mascot is a pony? The story is that Django comes with so many things you want: an ORM, all sorts of middleware, the admin site… "What else do you want, a pony?" Well, Gunicorn stands for "Green Unicorn" - obeythetestinggoat.com
- Nginx is the front face for your server.
- Gunicorn runs multiple django projects(each project is a wsgi application powered by Gunicorn) in a single server(say Ubuntu).
Every request comes to nginx and asks for which gunicorn application should it go and it redirects it.
NOTE - Gunicorn cannot serve static files automatically as your local django server does. So you will need nginx for that again.

- 463
- 3
- 18
In production nginx works as reverse proxy. It means users will hit nginx from browser and nginx will forward the call to your application. Hope this helps.

- 543
- 4
- 20