What is a reverse proxy?

A reverse proxy is a doorman👮🖐️ between the browser and the application server. It takes each request on the server's behalf and passes it to the right server behind it. The only thing a user ever sees is the reverse proxy. The apps and containers behind it stay out of sight🙈.

What does a reverse proxy do when you open a website?

When you open https://example.com, the reverse proxy does four jobs before the page shows up💪.

  • It shows its certificate
  • Unlocks the encrypted request
  • Passes the request to the right container
  • Locks the HTML that comes back before sending it to the browser.

The example here is a common setup: an app (say, Next.js) running in a Docker container behind the reverse proxy. We'll follow one request from the moment it arrives until the HTML goes back.

Let's see how they work:
What Is a Reverse Proxy Doing Behind the Browser?

1. Look up the IP with DNS:

The browser asks a DNS server, “What is the IP for example.com?” and gets back the server's IP (for example, 203.0.113.10). The reverse proxy is not involved yet.

2. Connect to port 443:

The browser connects to that IP on port 443. A port is a numbered door into one server, and HTTPS normally uses 443. The reverse proxy is the first thing waiting at that door(Yay!finally👏).

3. Show the certificate and share a key (the TLS handshake):

The reverse proxy holds a public/private key pair that was made ahead of time, when the certificate was set up. The only key made fresh for each connection is the session key you'll meet in a moment.

The reverse proxy hands its certificate (which contains the public key) to the browser. The browser checks the certificate's domain name and expiry date, then makes a throwaway “session key”, puts it in a box, locks the box with the public key, and sends it.

A public key and a private key are a pair: a box locked with the public key can only be opened with the matching private key. So only the reverse proxy, which holds the private key, can open this box. Nobody along the way can.

The reverse proxy opens the box with its private key and takes out the session key. Now the browser and the reverse proxy hold the same session key, and everything after this is encrypted with it.

4. Open the request and read it (TLS termination):

Once the key is shared, the browser builds an HTTP request, locks it with the session key, and sends it. The reverse proxy opens it with the session key (decrypts it) and reads what is inside. The request is plain text like the one below. Only now does the proxy know which domain and which path it is for.

5. Choose where to send it (routing):

The reverse proxy uses the Host header (the domain the user typed) and the path to look up a destination in a table it was given, for example example.com → app:3000, and passes the request to that app. The destination is an address inside the server, not the public domain. It also adds the client's original IP to an X-Forwarded-For header.

6. The app builds the HTML:

Next.js, running on Node.js inside the container, handles the request and sends HTML back to the reverse proxy.

7. Lock it and send it back:

The reverse proxy takes the HTML from the app (it isn't encrypted yet), locks it with the session key, and sends it to the browser. The browser opens it with the same key and shows the page(Phew, about time😮‍💨 That was a lot of key things...).

Why lock everything so tightly?

So many key things!🔑😮‍💨 Why do we need this process...? Actually, traffic between a browser and a server passes through many machines that belong to other people: Wi-Fi, routers, internet providers. If the connection isn't locked (plain HTTP), anyone along the way can do the following:
Why lock everything so tightly?

OK, then why two kinds of keys?

A browser and a server that meet for the first time can't agree on a secret word ahead of time. So the browser locks a box with the public key, which is fine for anyone to see, and only the server holding the private key can open it. That is how the session key travels safely. Once it arrives, both sides use the same fast session key for the rest of the conversation.
The certificate matters for the same reason. A fake server could hand over its own public key. The browser checks the certificate (the signature of a certificate authority such as Let's Encrypt, the domain name, and the expiry date) to decide whether the server is the real one.

Is a reverse proxy just a doorman👮🖐️? Nah, it's a guardian🎠⚔️, I would say!

Keep the original IP with X-Forwarded-For

From the app's point of view, every request comes from the reverse proxy. If nothing is done, the access log shows the same IP for everyone, and you can't block an IP or look into abuse. So the reverse proxy adds X-Forwarded-For: when it forwards the request, and the app reads that header for logs and security checks. A client can fake this header, so only trust values added by a reverse proxy you trust.

Examples of reverse proxies

Reverse proxies come as dedicated software and as cloud services. These are the common ones.
List of Reverse proxies

How this connects to AI🤖

Apps that use AI have the same kind of doorman in front. It is usually called an AI gateway🤖⚔️(or you can say an LLM proxy): a reverse proxy placed in front of LLM APIs. It is still a reverse proxy underneath, but it does more than pass requests along. It also handles authentication, usage limits, and switching between models, so people call it a gateway, like an entrance that manages everything coming in.
How this connects to AI?

  • They are slow. A normal page comes back in milliseconds. An LLM answer can take many seconds, because the model writes it a little at a time. If every part of the system waits for the whole answer, the user stares at a blank screen, and connections that sit idle too long get cut by timeouts. A reverse proxy set up for this passes each piece along as it arrives (streaming), so the text starts showing right away. It can also allow longer timeouts for these requests, and retry or switch to another model when one provider fails.

  • They cost money. A normal page costs almost nothing per visit. An LLM call is billed by the amount of text (tokens), so one buggy loop or one abusive user can run up a large bill. Because the reverse proxy sits at the front door, it can limit how many requests each user or app may send, answer repeated questions from a cache instead of calling the API again, and record the usage and cost for each user or app.

  • A leaked API key can do real damage. An API key works like a credit card number: anyone who has it can spend your money. If every app, script, and teammate holds the real key, one leak in a repository or a log exposes it. With a gateway, only the gateway holds the real key. Apps get an internal key instead, and you can limit, replace, or cancel that internal key on its own without touching the real one.

For instance, here is what the key problem looks like with three apps.
Without a gateway. The three apps each call OpenAI directly. The real key lives in three places.

# App A .env OPENAI_API_KEY=sk-real-key # App B .env OPENAI_API_KEY=sk-real-key # App C .env OPENAI_API_KEY=sk-real-key 

With a gateway. The apps call the gateway instead. The gateway checks the internal key, swaps in the real key, and forwards the request to OpenAI. The real key lives in one place.

# Apps A, B, C .env (a different value for each app) GATEWAY_KEY=internal-key # Gateway .env OPENAI_API_KEY=sk-real-key 

Either way, the key sits in an environment variable. What changes is how many places hold the real key. To replace it, you update one place instead of three. If App B leaks its key, you cancel only App B's internal key, and the real key stays safe.

The app talks only to the gateway, and the gateway forwards the request to each company's LLM API. The app doesn't have to hold the API key. Rate limits, caching, usage and cost records, and switching between models all happen in one place. LiteLLM Proxy and Cloudflare AI Gateway are examples of this kind of gateway.

Wrapping up

A reverse proxy is easy to forget about, but it is the guardian that holds up a website's safety, speed, and day-to-day running. In the example.com flow we followed, the reverse proxy did all of this by itself:
• Showed the certificate and encrypted the connection with HTTPS (TLS termination)
• Looked at the domain and path and sent the request to the right container
• Passed the original client IP to the app
• Kept the certificate renewed automatically

Because all of this sits in one place, the app can focus on its own logic. Thanks, our guardian🎠⚔️!