Web Servers, Application Servers, and Reverse Proxies

Beginner · 13 min

Why your framework's own server never faces the internet directly, and what Nginx or Apache is actually doing in front of it.

Before thisWeb Frameworks and the Request-Response Cycle

TechWorld with Nana · 13 min

Web Servers, Application Servers, and Reverse Proxies

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TechWorld with NanaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An application server (Gunicorn, uvicorn, Puma, or Node's own built-in HTTP server) is the process that actually runs your framework's code and turns a request into your response.
  • A reverse proxy or web server (Nginx, Apache, Caddy) sits in front of that process and handles the parts of talking to the internet that your framework was never built to handle safely.
  • You almost never expose the application server directly, because it's optimized for running your code fast, not for surviving slow, malicious, or just badly-behaved clients on the open internet.
  • The reverse proxy typically owns TLS termination and serving static files, which frees the application server to do nothing but execute your application logic.

Two processes, two different jobs

Every backend framework ships with some way to actually run: Django and Flask apps run under Gunicorn or uvicorn, Rails runs under Puma, a plain Node.js app can just call http.createServer() and start listening. Whatever you call it, this is the application server — the process that holds your compiled or interpreted code in memory, matches an incoming request to the right handler, runs your business logic, and hands back a response.

It would seem like that’s the whole story. You have a process that speaks HTTP and runs your code — why would you need anything else between it and the internet? The answer is that “speaks HTTP and runs your code” is a much narrower job than “safely serves the public internet,” and almost every application server is optimized for the first at the expense of the second.

What an application server is bad at

An application server’s job is to execute your handler functions as fast as possible, usually by keeping a pool of worker processes or threads that each hold a chunk of your app in memory. That design is a liability the moment a client on the internet starts behaving badly — and on the internet, some fraction of clients always do, whether through malice or just a bad connection.

Picture a client that opens a connection and then sends your request one byte every few seconds — maybe deliberately (a “slow loris” attack aimed at exhausting your worker pool) or just because they’re on a terrible mobile connection. Your application server has a limited number of workers. If each slow client ties one up for the full duration of that crawl, it doesn’t take many of them to exhaust your worker pool and leave zero capacity for legitimate, fast requests. Gunicorn’s own documentation is blunt about this: it explicitly recommends never exposing it directly to untrusted clients, for exactly this reason.

An application server also typically has no concept of TLS, no built-in logic for efficiently streaming a static file off disk, and no interest in handling the connection-level housekeeping — keep-alive management, header size limits, malformed request rejection — that a public-facing server needs. None of that is a flaw in Gunicorn or Puma. It’s just not what they were built to do.

What a reverse proxy does instead

A reverse proxy — Nginx, Apache, Caddy — is a separate process built for exactly the job your application server is bad at. It’s the thing that actually binds to port 443 and faces the internet. It terminates TLS, so your application server only ever has to deal with plain HTTP over a private, internal connection — no certificates, no cipher negotiation duplicated across every worker. It buffers slow or partial requests, only handing the application server a complete, well-formed request once the slow part is done, which means a client trickling in data ties up a cheap proxy connection instead of a precious application worker. It serves static files (images, CSS, JS bundles) straight off disk, a job web servers are extremely efficient at, without ever bothering your application code at all.

Internet → Nginx (TLS, static files, buffering) → Gunicorn (your code) → your app logic

The practical setup is almost always the application server listening only on localhost or a Unix socket — not reachable from outside the machine at all — with the reverse proxy as the only thing the internet can actually talk to. If someone bypasses the proxy and hits the application server directly, there’s no path there: it’s simply not listening on a public interface.

Why “reverse,” and how this differs from load balancing

The name comes from contrast with a plain (forward) proxy, which sits in front of clients going out to the internet — a company’s outbound web filter, for instance. A reverse proxy sits in front of servers, intercepting inbound traffic on their behalf; the embedded video above walks through that forward-vs-reverse distinction directly, along with a concrete look at why a Node.js app ends up needing Nginx in front of it rather than facing the internet on its own.

It’s worth being precise about scope here, because the terms get blurred together in casual conversation. A reverse proxy’s defining job is standing between the internet and your backend process — that’s true even with a single backend instance and nothing to balance. Load balancing — spreading traffic across multiple backend instances, with health checks and routing rules — is a related but separate concern that typically only matters once you’re running more than one copy of your app. The same Nginx config can do both jobs at once, which is part of why they get conflated, but a single-instance service still needs the reverse proxy piece even with nothing to balance. The load-balancing and gateway-routing side of this, including how that scales across many services, is covered in reverse proxies and API gateways — this lesson is about why the two processes exist at all, not about running a fleet of them.

Check yourself

  1. 01What's the actual difference between an application server and a web server/reverse proxy?

    The application server is the process running your framework's code in your language's runtime — it turns an incoming request into a response by executing your handlers. The web server/reverse proxy is a separate, general-purpose process that accepts internet traffic, handles TLS and static files, and forwards the requests it can't answer itself to the application server.

  2. 02Why not just let Gunicorn or uvicorn listen directly on port 443 and skip Nginx entirely?

    Application servers are built and tuned to run your code, not to defend against the open internet — slow clients trickling in bytes one at a time, malformed requests, thousands of idle connections. A reverse proxy is purpose-built for exactly that job, buffering and filtering traffic before it ever reaches your application code.

  3. 03What does 'TLS termination' mean, and why hand it to the reverse proxy instead of the application server?

    TLS termination is where the encrypted HTTPS connection is decrypted back into plain HTTP. Handing that job to the reverse proxy means your application server only ever has to speak plain HTTP on an internal connection, and certificate renewal and cipher configuration live in one place instead of being duplicated across every application server process.

  4. 04Why does a reverse proxy usually serve static files instead of the application server?

    Serving a file from disk is a generic, CPU-cheap operation that a web server like Nginx is extremely optimized for, while your application server's worker processes are a comparatively scarce resource meant for running your actual code — using one of them to stream a CSS file is wasted capacity that could have handled a real request instead.

  5. 05Is a load balancer the same thing as a reverse proxy?

    They overlap but answer different problems: a reverse proxy's core job is standing between the internet and your backend, while a load balancer's core job is spreading traffic across multiple backend instances — a single reverse proxy in front of one backend process doesn't need to load-balance at all, though the same software often does both jobs at once.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark