Linux Labs

Web stack / Stage 2 of 9

Reverse proxy and 502

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 8 · on a machine · 6 min

Two requests instead of one

nginx stands between you and the application. This step shows where the 502 comes from.

nginx does not keep the shop's pages. It works as a reverse proxy: it takes the client's request, sends its own request to the application and passes the application's answer back to the client. The application behind a proxy is called the upstream. The request's path splits into two legs — client → nginx and nginx → application — each with a connection of its own.

The location block inside server says where to send the requests:

nginx
server {
    listen 80;
    server_name api.example.test;
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

location / covers the paths that start with /, so any path. proxy_pass is the application's address: nginx will connect to 127.0.0.1, port 3000.

A 502 Bad Gateway answer means the first leg worked: nginx got the request and answered it itself. It got no answer from the application — the second leg broke. nginx writes why to error.log, in a line with the words while connecting to upstream.

Exercise · look

Find where nginx connects

Request the site once more, read the last line of the nginx error log and see which ports on the machine are listening.

Sign in and start The machine is available after signing in
Ubuntu 24.04
Loading…