Nginx Error Pages and SPA Fallback Without Hiding API Errors
An error page is not a success response
An error response has both a status and a body. Use error_page when you want a useful explanation while keeping a failure status. Use an internal-only location for a shared error document if it should not be browsed as a standalone page. All examples assume a static root named site within the lab prefix.
location /docs/ { try_files $uri =404; error_page 404 /errors/404.html;}location = /errors/404.html { internal;}If /docs/missing.html does not exist, the server internally handles /errors/404.html and, assuming the error document is served successfully, the client still receives 404. A direct external visit to that internal location is denied with 404. A response override such as error_page 404 =200 /errors/404.html; would instead turn a failure into success; that is not appropriate for ordinary missing documents. If the error handler itself fails, do not assume the original response is preserved.
Scope the SPA route to the application
A single-page application (SPA) may handle a route such as /app/settings in browser code even though no settings file exists. Serve its HTML entry document for those navigational paths, but reserve separate locations for assets and APIs. The following fixture has a static entry page as a stand-in for an SPA; it does not implement a client router.
location = /app { return 308 /app/; }location /app/assets/ { try_files $uri =404; }location /app/ { try_files $uri /app/index.html; }location = /app/index.html { try_files $uri =404; }location = /api/status { default_type application/json; return 200 '{"ok":true}';}location /api/ { default_type application/json; return 404 '{"error":"not found"}';}An existing asset is served as its own file. A missing asset receives 404. An application route such as /app/settings receives the entry document, while the API uses its own JSON contract. The exact entry-document location stops a missing index from repeatedly falling back to itself. Longest-prefix selection isolates /app/assets/ from /app/ in this simple server with no competing regex. Restrict fallback to appropriate navigation routes; a production application may also require method checks and application-specific routing.
Understand method changes on error handling
location = /method-uri { error_page 404 = /method-target; return 404;}location = /method-target { internal; return 200 "$request_method\n";}location = /method-named { error_page 404 = @method_target; return 404;}location @method_target { return 200 "$request_method\n"; }This diagnostic example intentionally uses = so the final handler decides the status. An error redirect to a URI converts methods other than GET and HEAD into GET. A named error handler keeps the URI and method. A POST to /method-uri is therefore predicted to return GET, while POST to /method-named returns POST. These handlers only echo a synthetic method; they are not production error policies. Do not use a generic HTML error fallback for application writes or assume that a failed request can safely be retried.
Verify the HTTP contract
curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18089/app/settingscurl --noproxy '*' --max-time 2 -i http://127.0.0.1:18089/app/assets/missing.csscurl --noproxy '*' --max-time 2 -i http://127.0.0.1:18089/api/missingExpect 200 HTML, 404, and 404 JSON. Check status and media type as well as the response text. The next chapter shows how logs help identify the route and filesystem path that produced a response.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Error status and application fallback
Predict the file or response and explain the selected behavior.
What status should a successfully rendered ordinary custom 404 retain?
Why is the error document location marked internal?
Which response fits a missing /app/assets/theme.css?
What should /api/missing return in the example?
For a POST handled by the illustrated error_page URI redirect versus named fallback, which methods reach the handlers?