Premier League Predictor: FastAPI & PostgreSQL — Chapter 12, Exercise 1
====================================================
TASK
Write the nginx location /predictor/ block from this chapter, update at
least two real fetch() calls from earlier chapters to relative paths,
add the tag, and confirm a request that
previously would have resolved to the site root now correctly reaches
the proxied FastAPI app instead.
SOLUTION
The nginx block, added inside the existing server {} for example.com:
location /predictor/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Two real fetch() calls updated from absolute to relative, taken
directly from earlier chapters:
Chapter 4 (fixtures.js):
Before: fetch(`/api/seasons/${seasonId}/teams`)
After: fetch(`api/seasons/${seasonId}/teams`)
Chapter 7 (table.js):
Before: fetch(`/api/seasons/${seasonId}/table`)
After: fetch(`api/seasons/${seasonId}/table`)
The base tag, added to the
of every static page:
Confirming the fix: before these changes, loading
https://example.com/predictor/index.html and letting fixtures.js run
its old fetch('/api/seasons/1/teams') call would resolve to
https://example.com/api/seasons/1/teams — a URL nginx has no matching
location for (only / and /predictor/ exist), so it falls through to
the Astro site's own catch-all and returns Astro's 404 page instead of
real team data.
After the fix: the same page load, now containing , runs the updated fetch('api/seasons/1/teams')
(no leading slash). The browser resolves that relative path against
the base href, producing
https://example.com/predictor/api/seasons/1/teams — which DOES match
nginx's own /predictor/ location block, gets the prefix stripped by
proxy_pass, and is forwarded to FastAPI as a plain
/api/seasons/1/teams request, returning the real, correct team data.
WHY THIS WORKS AS AN ANSWER
----------------------------
It provides the real, complete nginx block, shows two genuine before/
after fetch call changes from named earlier chapters, and traces the
exact URL resolution both before and after the fix — confirming
concretely which real URL a stale absolute path resolves to (a broken
one) versus the fixed relative path (the correct, proxied one).