Gunicorn and Nginx in a Nutshell

TAGS: DEPLOYMENT

When hosting a Python web app in production you can’t get around using a WSGI server and a web server.

Gunicorn and Nginx are solid and popular options - but what are those two apps? Why do people run both, and not just one of them?

Nginx and Gunicorn work together

Nginx is where requests from the internet arrive first. It can handle them very quickly, and is usually configured to only let those requests through, which really need to arrive at your web application.

Gunicorn translates requests which it gets from Nginx into a format which your web application can handle, and makes sure that your code is executed when needed.

They make a great team! Each can do something, which the other can’t. Let’s look at their strengths in detail, and how they complete each other.

Nginx

Nginx is a web server and reverse proxy. It’s highly optimized for all the things a web server needs to do. Here are a few things it’s great at:

  • Take care of domain name routing (decides where requests should go, or if an error response is in order)
  • Serve static files
  • Handle lots of requests coming in at once
  • Handle slow clients
  • Forwards requests which need to be dynamic to Gunicorn
  • Terminate SSL (https happens here)
  • Save computing resources (CPU and memory) compared to your Python code
  • And a lot more, if you configure it to do so (load balancing, caching, …)

Things Nginx can’t do for you:

  • Running Python web applications for you
  • Translate requests to WSGI

Gunicorn

Once Nginx decides, that a particular request should be passed on to Gunicorn (due to the rules you configured it with), it’s Gunicorn’s time to shine.

Gunicorn is really great at what it does! It’s highly optimized and has a lot of convenient features. Mostly, its jobs consist of:

  • Running a pool of worker processes/threads (executing your code!)
  • Translates requests coming in from Nginx to be WSGI compatible
  • Translate the WSGI responses of your app into proper http responses
  • Actually calls your Python code when a request comes in
  • Gunicorn can talk to many different web servers

What Gunicorn can’t do for you:

  • Not prepared to be front-facing: easy to DOS and overwhelm
  • Can’t terminate SSL (no https handling)
  • Do the job of a webserver like Nginx, they are better at it

Gunicorn is just one of many valid WSGI servers. Your app doesn’t care which one you use, and Nginx doesn’t care either. But Gunicorn is a mighty- fine choice!

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.