API gateway responsibilities
What belongs at the gateway?
An API gateway is a controlled entry point for application requests. It selects a backend using a hostname or path, may enforce common access policies and forwards accepted traffic. In a report service, the gateway can require a valid identity while the report application checks whether that identity can read the particular report. Authentication answers who is calling; authorization answers what that caller may do. The application remains responsible for object ownership, schema validation and business rules.
location = /health { return 200 "ready\n"; }location /api/ { client_max_body_size 64k; proxy_pass http://127.0.0.1:9000;}location / { return 404; }The exact health route returns a gateway-local marker, not a proof of backend readiness. The API prefix forwards to a fixed local backend and preserves the original URI when no URI is specified in proxy_pass. All other paths return 404. This example does not authenticate anyone: it is only a routing boundary. The body limit can produce 413 for a request exceeding the threshold; it does not validate JSON structure.
Restrict methods without confusing access and semantics
location /reports/ { limit_except GET { deny all; } proxy_pass http://127.0.0.1:9000;}GET also permits HEAD under limit_except. The access module denies other methods with 403 here. If your API contract requires 405 with an Allow header, implement that explicitly in the application or a reviewed gateway rule. Never interpret method filtering as authorization for individual reports. A GET request can still expose private data.
Choose an authentication mechanism
location /staff/ { auth_basic "Staff area"; auth_basic_user_file /path/to/approved-password-file; proxy_pass http://127.0.0.1:9000;}HTTP Basic uses a username/password credential encoding, not encryption. Use TLS, a protected password-hash file outside the web root and reviewed password storage; the path above is a placeholder. For centralized identity, use a separately trusted authentication service or supported module. Nginx commercial ngx_http_auth_jwt_module (1.11.3+) validates JWTs; ngx_http_oidc_module (1.27.4+) implements the OIDC authorization-code flow. These are not assumed available in Open Source. njs integrations use separately installed script modules and reviewed identity libraries or services; they are not the native commercial directives. JWT validation requires an approved algorithm, trusted keys, issuer, audience and time constraints. Decoding a token is not validation. OIDC sign-in does not itself establish permission to every API object.
Practice
Sketch client → gateway → authentication service → report API. Mark which component rejects an oversized body, an anonymous request and a report belonging to another user. Next: authentication subrequests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
API gateway responsibilities
Design an Nginx API gateway boundary and distinguish routing, authentication, authorization and request limits.
Which layer must check report ownership?
What does limit_except GET allow without its nested restriction?
What does client_max_body_size enforce?
Why is TLS needed with Basic authentication?
Which feature is a native commercial module since 1.27.4?