API header and error contracts
Design the boundary for errors as well as success
The gateway should not turn access errors into successful HTML responses. Keep 401, 403, 404, 413 and unexpected 5xx distinguishable. Use a documented error body format when needed, but preserve status and authentication challenges. For protected APIs, use no-store where responses should not be retained and avoid logging credentials, cookies or sensitive URLs.
add_header Cache-Control "no-store" always;add_header Vary Origin always;The always parameter applies headers to errors too. On older versions, a location with its own add_header declarations does not inherit the parent set; check actual inheritance and avoid duplicate upstream headers. A response header is not a guarantee of application authorization.
CORS is a browser policy, not an access check
An origin consists of scheme, host and port. Cross-origin resource sharing (CORS) allows browser scripts to read selected cross-origin responses. Some requests trigger an OPTIONS preflight before the actual request. A successful preflight describes allowed browser behavior; it does not grant API permission. Command-line clients are not constrained by browser CORS rules.
map $http_origin $allowed_origin { default ""; "https://ui.test" "https://ui.test";}add_header Access-Control-Allow-Origin $allowed_origin always;add_header Vary Origin always;add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS" always;add_header Access-Control-Allow-Headers "Authorization" always;location = /api/report { if ($request_method = OPTIONS) { return 204; } # Actual GET/HEAD access policy and proxy go here.}The empty mapped value omits the origin header for other origins. The fixture returns an empty 204 for OPTIONS, not protected data; a browser with an unapproved origin cannot read the response as CORS-approved. For an explicit cross-origin authorization header, the browser checks the allowed headers during preflight. This example does not enable credentialed cookie requests. Credentialed CORS needs an explicit approved origin and the appropriate credentials header, not a wildcard origin. Vary: Origin tells caches that the response can differ by origin. Place the actual access rules on GET/HEAD; a return before access handlers must never return protected content.
Verify a negative case before trusting the gateway
curl --noproxy "*" -i -H "Host: gateway.test" http://127.0.0.1:18320/api/reportcurl --noproxy "*" -i -H "Host: gateway.test" -H "Authorization: Bearer lab-editor" http://127.0.0.1:18320/api/reportExpect 401 and an authentication challenge from the first request, and the synthetic report from the second. The lab also checks a fabricated identity header, a reader token, a failing or slow auth service, direct internal-route requests, method denial, oversized body and allowed/disallowed origins. The mock backend trusts the gateway within an isolated environment; real deployments must enforce that trust using network restrictions or authenticated service-to-service transport. A browser-specific CORS test is separate from header checks.
Practice
Write a test plan that would catch a gateway mistakenly forwarding a client identity, a proxy returning 200 for a missing API path, and an auth service outage. Next: rate limiting and connection control.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
API header and error contracts
Preserve HTTP status and trusted identity contracts, design restrictive CORS and test negative API cases.
What does an allowed CORS preflight establish?
Why send Vary: Origin with a dynamic allowed origin?
What should a direct external request to an internal auth location return in the lab?
Which test best checks identity header trust?
Which status best describes an oversized request body?