Linux Labs

Web stack / Stage 2 of 9

Reverse proxy and 502

Not running Starting usually ~20 s
Leave the lesson

Step 5 of 8 · on a machine · 7 min

The path the application sees

The proxy passes the page path on to the application too. This step shows how a single slash in proxy_pass changes that path.

location /api/ catches the paths that start with /api/. If nothing follows the port in proxy_pass, the application gets the path as it is. If a path follows the port — even just / — nginx puts it in place of the matched prefix /api/:

proxy_pass in location /api/ The request /api/items arrives as
http://127.0.0.1:8002 /api/items
http://127.0.0.1:8002/ /items

When several location blocks fit a path, nginx takes the one with the longer prefix: /api/items goes to location /api/, not to location /.

Check yourself 1 question

location /api/ has proxy_pass http://127.0.0.1:8002/;. With which path will the request /api/orders/7 reach the application?

  • That would happen with nothing after the port. Here the port is followed by /, and nginx put it in place of the matched prefix /api/.

  • The / in proxy_pass replaces the matched prefix, not the whole path: the tail orders/7 stays.

  • Only the prefix matched by location, /api/, is replaced; the tail orders/7 goes through as it is.

The port in proxy_pass is followed by the path /, and nginx puts it in place of the prefix matched by location, /api/. The tail orders/7 goes through as it is, so the application gets /orders/7.

The warehouse has started its own application, lab-stock, on 127.0.0.1:8002. Its addresses start with /items: /items is the list of goods and /items/<number> is one item. The shop wants to serve them under its own name with the /api/ prefix and keep every other address of the site with its own application. Both applications print the path they got, so their answer shows what reached each of them. You are done when the warehouse gets the addresses without the prefix and the shop gets all the rest.

Exercise · do it

Hand /api/ to the warehouse

Make requests to http://shop.lab.test/api/items… reach the warehouse application lab-stock as /items…, while every other address of the site still goes to the shop. The site is /etc/nginx/sites-available/shop.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Hand the /api/ addresses to the warehouse application not done yet
  • Keep every other address with the shop not done yet

Step goals

0 of 2
  • Hand the /api/ addresses to the warehouse application — not done yet
  • Keep every other address with the shop — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Hand the /api/ addresses to the warehouse application — not done yet
  • Keep every other address with the shop — not done yet
Loading…