What is a JSON error response in the context of nginx?
A JSON error response is a structured message sent by nginx to the client when an error occurs, formatted in JSON to make it easy for applications to understand and handle the error.
Click to reveal answer
intermediate
How do you configure nginx to return a JSON error response?
You configure nginx to return JSON error responses by using the 'error_page' directive to specify a custom error page and then serving a JSON file or inline JSON content with the correct 'Content-Type: application/json' header.
Click to reveal answer
beginner
What HTTP header must be set to indicate a JSON error response?
The HTTP header 'Content-Type: application/json' must be set to indicate that the response body contains JSON data.
Click to reveal answer
beginner
Example of a simple JSON error response body?
{"error": {"code": 404, "message": "Not Found"}}
Click to reveal answer
intermediate
Why use JSON error responses instead of plain text in APIs?
JSON error responses allow clients to easily parse and handle errors programmatically, improving automation and user experience compared to plain text messages.
Click to reveal answer
Which nginx directive is used to specify a custom error page?
Alocation
Bproxy_pass
Cerror_page
Dreturn
✗ Incorrect
The 'error_page' directive tells nginx which page or response to serve when a specific error occurs.
What Content-Type header should be set for JSON error responses?
Aapplication/json
Btext/html
Ctext/plain
Dapplication/xml
✗ Incorrect
The 'application/json' header indicates the response body is JSON formatted.
What is the main benefit of JSON error responses in APIs?
AThey are easier for humans to read
BThey improve network speed
CThey reduce server load
DThey allow clients to parse errors programmatically
✗ Incorrect
JSON error responses let client programs understand and react to errors automatically.
Which HTTP status code is commonly used for 'Not Found' errors?
A200
B404
C301
D500
✗ Incorrect
404 means the requested resource was not found on the server.
In nginx, how can you serve a JSON error response inline without a separate file?
AUsing 'return' with JSON string and setting Content-Type
BUsing 'proxy_pass' to a JSON server
CUsing 'rewrite' directive
DUsing 'listen' directive
✗ Incorrect
You can use 'return' with a status code and JSON string, plus 'add_header' to set Content-Type.
Explain how to configure nginx to return a JSON error response for a 404 error.
Think about how nginx handles error pages and headers.
You got /3 concepts.
Why is it important to set the Content-Type header correctly when returning JSON error responses?
Consider how clients read and use the response data.
You got /3 concepts.
Practice
(1/5)
1. What is the main benefit of sending JSON error responses in nginx?
easy
A. They automatically fix server errors without manual intervention.
B. They make error information easy for applications to read and process.
C. They reduce the size of error messages to save bandwidth.
D. They prevent users from seeing any error messages.
Solution
Step 1: Understand JSON error response purpose
JSON error responses format error details in a way apps can easily parse and handle.
Step 2: Compare options
Only They make error information easy for applications to read and process. correctly states the benefit of JSON error responses; others are incorrect or unrelated.
Final Answer:
They make error information easy for applications to read and process. -> Option B
Quick Check:
JSON error responses = easy app reading [OK]
Hint: JSON errors help apps read errors clearly [OK]
Common Mistakes:
Thinking JSON errors fix server issues automatically