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 /.
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
/inproxy_passreplaces the matched prefix, not the whole path: the tailorders/7stays. -
Only the prefix matched by
location,/api/, is replaced; the tailorders/7goes 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.
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.
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