FDE Times dataFDE jobs open 932▼14 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 62Companies hiring 202Remote-friendly 24%Median US pay $201kTop hirer Databricks 92
中文VI

The newspaper of the Forward Deployed Engineer

Guides

Behind a client’s load balancer, every user shares one IP

Lost client addresses, dropped sessions and misleading health checks are all fixable once you know what L4, L7 and a reverse proxy can see.

ShareLinkedInFacebookX

In brief

  • L4 sees only addresses and ports; L7 can read HTTP headers. Find out which one the client runs before you write the first line of configuration.
  • Behind a proxy, the server sees only the IP of the last proxy. Trust X-Forwarded-For only from proxies you have identified as trustworthy.
  • A service is easy to place behind a load balancer when it keeps no sessions in RAM, has a real health check endpoint and lets the load balancer handle TLS if the client wants that.
Diagram of a request's path: the browser carries the original IP and reaches the client's L7 load balancer (reads URL and cookie, adds X-Forwarded-For), then your NGINX (trusts headers only from 10.20.0.0/24, highlighted), then the app, which sees the real IP. Below are three common faults: lost real IP, fixed by trusting X-Forwarded-For only from the load balancer's IP range; sessions jumping around, fixed by moving sessions to a shared store such as Redis; and a misleading health check, fixed by writing a /healthz that genuinely checks the app.
A request passes through the client's load balancer before reaching your NGINX. At NGINX, you can fix lost real IPs, jumping sessions and misleading health checks.

Within an hour of the switch, every request in the log carries the same IP address. The per-IP rate limit has blocked an entire department, and users are occasionally logged out in the middle of a conversation. Yet the code has no bugs.

This hypothetical deployment is at a bank, on an engineer’s third day. The internal document Q&A service had been running well on staging until the infrastructure team sent a message: all traffic must go through the bank’s load balancer, no exceptions. What the engineer is missing is an understanding of how the client’s network sees each request.

Situations like this are everyday work for an FDE. You rarely get to build the infrastructure from scratch. You have to fit a service into an existing system that other people operate under their own rules. Doing so means distinguishing clearly between three things: layer 4 load balancers, layer 7 load balancers and reverse proxies.

What does the client’s load balancer see?

According to F5’s glossary, a layer 4 load balancer routes using information from the transport layer, meaning addresses and ports, and does not read packet contents. It simply passes a TCP stream to a server, with no knowledge of the URL or cookies inside.

A layer 7 load balancer makes decisions based on characteristics of the HTTP header, such as the URL or cookies. Because it can read HTTP, it can send /api to one cluster and /static to another. It can also insert headers before passing the request inward.

This matters because each layer determines what your service receives, including whether there is an X-Forwarded-For header inserted by a proxy. Do not infer how TLS is handled from the label L4 or L7 alone. Whether decryption happens at the load balancer or in your service depends on the client’s configuration, so you have to ask.

A reverse proxy is not a load balancer

The two terms are often confused. A reverse proxy takes a request from a client and forwards it to a server that can handle it, while a load balancer distributes requests across a group of servers. A reverse proxy is therefore useful even when there is only one server behind it.

Even if the client already has a load balancer, you should still run a reverse proxy of your own, such as NGINX, directly in front of the app. That is the point you control: it is where you recover the real IP, set timeouts and answer health checks. You do not have to ask the infrastructure team to change their equipment every time a setting needs to move.

NGINX suits this role because of its architecture. The NGINX engineering blog explains that a common way to design network applications is to assign each connection a thread or process, an approach that incurs context-switching costs.

NGINX chose an event-driven architecture: a master process handles privileged work such as reading configuration and binding ports, while worker processes do the main work.

A worked example: recovering the user’s real IP

Back at the bank, a request now follows this path: employee’s browser → the bank’s L7 load balancer → your NGINX → the app. When a connection passes through any proxy, the server sees only the IP of the last one, so your log contains nothing but the load balancer’s address.

The remedy is the X-Forwarded-For header: the proxy inserts the client’s original IP into it before forwarding the request. The app reads that header to learn who is really calling.

But MDN warns that if you use this header for security purposes, such as rate limiting or IP-based access control, you must use only IPs added by a trusted proxy. The reason is that a client can send a forged header of its own.

Suppose the infrastructure team tells you their load balancer sits in the range 10.20.0.0/24. Your NGINX configuration would look like this:

# Trust X-Forwarded-For only when the request comes from the client's load balancer
set_real_ip_from 10.20.0.0/24;
real_ip_header   X-Forwarded-For;
real_ip_recursive on;

upstream app {
    server 127.0.0.1:8000;
}

server {
    listen 80;

    location /healthz {
        proxy_pass http://app;
    }

    location / {
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header Host              $host;
        proxy_pass http://app;
    }
}

The set_real_ip_from line is the most important in the whole configuration. Without it, anyone who sends X-Forwarded-For: 1.2.3.4 can impersonate someone else and get past your rate limit.

With it, NGINX trusts only values that come from the load balancer’s IP range. The exercise at the end shows how to check this with curl.

Why sessions jump, and why sticky sessions won’t save you

Next, the logouts. Suppose you run 3 instances and balance by source IP. HAProxy’s architecture documentation describes the method this way: the same IP always reaches the same server, as long as the number of servers does not change.

Both conditions can fail. If you hash on the IP that your layer sees, every request carries the load balancer’s address, so 3 instances become 1 overloaded instance. If you scale to 4 instances next week, the hash redistributes traffic, and many users are moved to a different server and lose the session held in RAM.

The more durable approach is to keep no sessions in the app. In a horizontally scaled design, servers sit behind the load balancer while sessions live in a shared data store that every app server can reach.

At a client site, that store is usually Redis or a database they already have. In the bank scenario, the two approaches compare as follows:

Situation Sticky by IP, sessions in RAM Sessions in a shared store
Every request carries the load balancer’s IP All 3 instances collapse into 1 overloaded instance Any instance can read the user’s session
Scaling from 3 to 4 instances The hash redistributes and many users are logged out Users stay logged in

Five steps before deployment

  1. Ask the infrastructure team three questions. Is the load balancer L4 or L7, where does TLS terminate, and what is the proxy’s IP range? The answers determine almost all of your configuration.

  2. Decide who handles TLS. The load balancer can take on encryption and decryption so that servers concentrate on their main job.

    If the client already does this, read X-Forwarded-Proto to find out whether the original request was HTTPS, rather than configuring certificates yourself.

  3. Write a /healthz that really checks. According to AWS documentation, Elastic Load Balancing checks the health of its targets and sends requests only to healthy ones.

    An endpoint that always returns 200 while the app has lost its database connection will keep sending users to a broken instance.

  4. Move sessions and temporary data to a shared store. The client can then add or remove instances without logging anyone out.

  5. Agree on timeouts. Ask how long the timeout is on their equipment.

    Requests that call an LLM can run longer than the default, and a request cut off midway is a very hard fault to find.

Common mistakes

The most common mistake is trusting every X-Forwarded-For without restricting its source, then using it for rate limiting. The second is turning on IP-based sticky sessions without realizing that all requests appear to come from the same address.

The third is assuming that a load balancer makes a reverse proxy unnecessary, so that every configuration change means waiting on another team’s ticket. The fourth is harder to spot: a health check that tests only whether the process is running, and never whether the app can serve requests.

For developers moving into FDE work, this is a skill to spell out on a CV. “Knows NGINX” says little. Describe a time when you put a service behind an existing proxy, preserved the real IP and ran multiple instances without losing sessions.

When a job description mentions “customer environment” or “on-prem deployment”, you can expect questions on these topics.

Exercise: try to fool your own proxy

Run NGINX with the configuration above in front of a small app that does nothing but write the client IP to a log. Set set_real_ip_from to the IP range of a machine you treat as the load balancer.

From a machine outside that range, run curl -H "X-Forwarded-For: 1.2.3.4" http://<proxy>/. If the log shows 1.2.3.4, your proxy is being fooled. If it shows the sender’s real IP, the configuration is correct.

Then delete the set_real_ip_from line, run the command again and compare the two log entries. You cannot change the client’s network, but a ten-minute test like this tells you whether your service will hold up inside it.

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
7 sources
Read next on the roadmap · Stage 5: DeploymentStop routing 100 GB a day through your app serverPresigned URLs let users upload straight to storage, leaving the application to decide who gets access and a CDN to serve static content.