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.
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:
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
docker network create labmakes a private network, anddocker run -dstarts a container in the background. - 2
--name app1names the container. The second--name app1, after the image, is an option ofwhoamiitself: it prints that name in its answer. - 3
-v $PWD/proxy.conf:/etc/nginx/conf.d/default.conf:roputs our file into the proxy container, read only. - 4
wget -qO- URLdownloads 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.
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?
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
curl -X POST -d 500 http://app2/healthsends aPOSTwith the body500. From then on,whoamianswers500on/health. - 2
-w "%{http_code} "prints the status code, and-o /dev/nullthrows 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:
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
docker restart frontstarts the proxy again, so that it reads the changed file.
Every request gets a 200, although app2 still answers 500.
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:
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
docker stop app2switches 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.