One front door for many programs

A reverse proxy is one address in front of many programs. It ends the client's connection and opens its own. It takes turns between the programs, but a health check that only proves the port is open sends requests into a broken one.

Part 8 of 9 · Networking4 min read
frontone front doorit opens its own connectionapp1app2500takes turnsapp1, then app2open but brokentry the other roomX-Forwarded-For:client 172.21.0.5it writes who askedin a headerproxy_next_upstream http_500retry on a real failurenot only on a closed portbackends see the proxythe client is in the header

A service often runs two copies of one program, and users must see one address. Something has to stand in front, take each request and give it to one of the programs.

A reverse proxy is that something. The word “reverse” separates it from a forward proxy, which stands in front of the clients, for example in an office network.

In the post office picture, the proxy is the front door of the office. Visitors see one door. Behind it, the programs are the workers who handle the letters. A proxy is useful for three reasons. Many copies share one address. When a copy fails, users may not notice. The proxy can also do shared work, such as the padlock from part 5.

clientsproxyone addressapp1app2programs behind it
Clients talk only to the proxy. The proxy gives each request to one of the programs behind it.

A proxy opens its own connection

We build a small proxy with nginx. The two programs behind it are copies of traefik/whoami, a tiny program that answers with information about the request it received. Save the proxy’s settings as proxy.conf:

upstream pool {
  zone pool 64k;
  server app1;
  server app2;
}
server {
  location / {
    proxy_pass http://pool;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

The upstream block lists the programs behind the proxy. proxy_pass gives each request to one of them, in turn. The proxy_set_header line writes the caller’s address into a header. The zone line makes nginx keep one shared count of whose turn it is. Without it, every helper process of nginx keeps its own count, and the turns become uneven. Start everything and send four requests:

Shell
docker network create lab 
docker run -d --name app1 --network lab traefik/whoami --name app1 
docker run -d --name app2 --network lab traefik/whoami --name app2
docker run -d --name front --network lab -v $PWD/proxy.conf:/etc/nginx/conf.d/default.conf:ro nginx:alpine 
docker run --rm --network lab alpine sh -c '
for i in 1 2 3 4; do
  wget -qO- http://front | grep -E "^(Name|RemoteAddr|X-Forwarded-For)" | tr "
" " "; echo 
done'
Name: app1 RemoteAddr: 172.21.0.4:43950 X-Forwarded-For: 172.21.0.5 
Name: app2 RemoteAddr: 172.21.0.4:37322 X-Forwarded-For: 172.21.0.5 
Name: app1 RemoteAddr: 172.21.0.4:43962 X-Forwarded-For: 172.21.0.5 
Name: app2 RemoteAddr: 172.21.0.4:37334 X-Forwarded-For: 172.21.0.5 

Here is what each marked part does:

  1. 1docker network create lab makes a private network, and docker run -d starts a container in the background.
  2. 2--name app1 names the container. The second --name app1, after the image, is an option of whoami itself: it prints that name in its answer.
  3. 3-v $PWD/proxy.conf:/etc/nginx/conf.d/default.conf:ro puts our file into the proxy container, read only.
  4. 4wget -qO- URL downloads a page and prints it. grep -E ... | tr "\n" " " keeps three lines and joins them into one.

We removed the lines that docker run -d prints. The two programs answer in turn. RemoteAddr is the caller that the program sees, and it is the proxy, 172.21.0.4, because the proxy ended the client’s connection and opened a second one. A server sees the proxy as its caller in the same way as it sees the router in part 6. The real client, 172.21.0.5, arrives only because the proxy wrote it into the X-Forwarded-For header. The addresses depend on the network that Docker creates, so yours can differ.

connection 1connection 2client172.21.0.5proxy172.21.0.4programsees: 172.21.0.4X-Forwarded-For: 172.21.0.5
The client talks to the proxy. The proxy opens a separate connection to the program, and writes the client's address into a header.

A program that listens and still fails

Make app2 fail. The program whoami has a /health address, and a POST with a number changes the status that it returns. Then ask the proxy for /health four times:

app2 still accepts connections. Will the proxy send traffic to it?

Shell
docker run --rm --network lab alpine sh -c '
apk add -q curl
curl -s -X POST -d 500 http://app2/health 
for i in 1 2 3 4; do curl -s -o /dev/null -w "%{http_code} " http://front/health; done' 
200 500 200 500 

Here is what each marked part does:

  1. 1curl -X POST -d 500 http://app2/health sends a POST with the body 500. From then on, whoami answers 500 on /health.
  2. 2-w "%{http_code} " prints the status code, and -o /dev/null throws the page away.

Every second request gets an error, and the proxy still treats app2 as healthy, because the port accepts connections. By default, nginx treats a request as failed only when it cannot connect or when it times out. A 500 is a valid HTTP answer, so nginx passes it to the client. One more line tells it that a 500 also means “try the other program”. Add it after the proxy_pass line:

    proxy_next_upstream error timeout http_500;

Restart the proxy with docker restart front and ask again:

Shell
docker restart front 
docker run --rm --network lab alpine sh -c '
apk add -q curl
for i in 1 2 3 4; do curl -s -o /dev/null -w "%{http_code} " http://front/health; done'
200 200 200 200 

Here is what each marked part does:

  1. 1docker restart front starts the proxy again, so that it reads the changed file.

Every request gets a 200, although app2 still answers 500.

frontapp1app2500app2 answers 500frontapp1app2200retry: the other roomfrontapp1app2200the client sees 200

The proxy gives the request to app2. The port is open, and the program answers 500.

A closed port is easy to see

A program that is switched off is different. Stop app2 and ask four times:

Shell
docker stop app2 
docker run --rm --network lab alpine sh -c '
for i in 1 2 3 4; do wget -qO- http://front | grep "^Name"; done'
app2
Name: app1
Name: app1
Name: app1
Name: app1

Here is what each marked part does:

  1. 1docker stop app2 switches that program off. The proxy still has it in its list.

The first line is docker stop printing the name of the container. No request failed. The proxy tried app2 once, got a refusal as in part 3, and gave the request to app1. By default, nginx then skips app2 for 10 seconds. A closed port is refused at once, so the proxy can try the other program.

Clean up with docker rm -f front app1 app2 and docker network rm lab.

Key points

  • A reverse proxy stands in front of other servers. Clients talk only to it, and it forwards each request to one of the servers behind it.
  • The program sees the proxy’s address. The client’s address arrives in a header such as X-Forwarded-For.
  • A closed port is found at once. A program that listens and still fails keeps receiving requests unless the proxy treats its errors as failures.
  • Retrying a failed request keeps users away from the error, but the broken program still needs a real health check.

Next, in part 9: how to find out where a request broke, one question at a time.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS