Bird
Raised Fist0
Nginxdevops~20 mins

JSON error responses in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this nginx error response configuration?
Given this nginx configuration snippet for JSON error response, what will be the exact JSON output when a 404 error occurs?
Nginx
error_page 404 = /custom_404.json;
location = /custom_404.json {
  default_type application/json;
  return 404 '{"error": "Not Found", "code": 404}';
}
A<html><body>404 Not Found</body></html>
BSyntaxError: invalid JSON
C{"error": "Page Missing", "code": 404}
D{"error": "Not Found", "code": 404}
Attempts:
2 left
💡 Hint
Look at the return directive and the content type set.
❓ Configuration
intermediate
2:00remaining
Which configuration snippet correctly returns a JSON error for 500 errors?
Select the nginx configuration snippet that correctly returns a JSON error response with status 500 and message "Internal Server Error".
A
error_page 500 = /500.json;
location = /500.json {
  default_type text/html;
  return 500 '{"error": "Internal Server Error", "code": 500}';
}
B
error_page 500 /500.json;
location /500.json {
  add_header Content-Type application/json;
  return 500 '{"error": "Internal Server Error", "code": 500}';
}
C
error_page 500 = /500.json;
location = /500.json {
  default_type application/json;
  return 500 '{"error": "Internal Server Error", "code": 500}';
}
D
error_page 500 = /500.json;
location = /500.json {
  default_type application/json;
  return 404 '{"error": "Internal Server Error", "code": 500}';
}
Attempts:
2 left
💡 Hint
Check the status code in the return directive and the content type.
❓ Troubleshoot
advanced
2:00remaining
Why does this nginx JSON error response return HTML instead of JSON?
You configured nginx as below to return JSON on 403 errors, but the client receives HTML instead. What is the most likely cause?
Nginx
error_page 403 = /403.json;
location = /403.json {
  return 403 '{"error": "Forbidden", "code": 403}';
}
AMissing default_type application/json directive in the location block
BThe return directive must use double quotes instead of single quotes
CThe error_page directive should not use '=' sign
DThe location block must be named /error/403.json instead of /403.json
Attempts:
2 left
💡 Hint
Check what content type nginx sends by default if not specified.
✅ Best Practice
advanced
2:00remaining
What is the best practice to serve consistent JSON error responses in nginx?
Which approach ensures all error responses (e.g., 400, 403, 404, 500) return JSON with proper status and message?
AUse a single error_page directive for all errors pointing to one JSON file and set default_type application/json globally
BDefine separate error_page and location blocks for each error code with JSON return and default_type application/json
CUse error_page directives without '=' and return HTML pages with embedded JSON scripts
DConfigure error_page to redirect to an external API that returns JSON error messages
Attempts:
2 left
💡 Hint
Think about how to customize each error code's message and status properly.
🧠 Conceptual
expert
2:00remaining
What happens if you omit the '=' sign in error_page directive for JSON error responses?
Consider this directive: error_page 404 /404.json; without '=' sign. What is the effect on the HTTP status code and response body?
ANginx issues an internal redirect to /404.json but keeps the original 404 status code
BNginx issues an external redirect changing the status code to 302
CNginx changes the status code to 200 and serves /404.json content
DNginx returns a syntax error and fails to start
Attempts:
2 left
💡 Hint
Understand the difference between internal redirect with and without '=' in error_page.

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

  1. Step 1: Understand JSON error response purpose

    JSON error responses format error details in a way apps can easily parse and handle.
  2. 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.
  3. Final Answer:

    They make error information easy for applications to read and process. -> Option B
  4. 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
  • Believing JSON errors hide all error messages
  • Assuming JSON errors reduce bandwidth significantly
2. Which nginx directive is used to specify a custom error page that returns JSON?
easy
A. error_page
B. listen
C. proxy_pass
D. server_name

Solution

  1. Step 1: Identify directive for custom error pages

    The error_page directive tells nginx which page to serve on specific errors.
  2. Step 2: Eliminate unrelated directives

    proxy_pass forwards requests, listen sets ports, server_name sets domain names; none define error pages.
  3. Final Answer:

    error_page -> Option A
  4. Quick Check:

    Custom error page directive = error_page [OK]
Hint: Use error_page to set custom error responses [OK]
Common Mistakes:
  • Confusing proxy_pass with error_page
  • Using listen or server_name for error handling
  • Forgetting to set content type for JSON
3. Given this nginx config snippet, what is the JSON response body for a 404 error?
error_page 404 /404.json;
location = /404.json {
  internal;
  default_type application/json;
  return 404 '{"error":"Not Found"}';
}
medium
A. 404 Not Found
B. Not Found
C. {"error":"Not Found"}
D. Error 404

Solution

  1. Step 1: Analyze the error_page and location block

    The 404 error triggers /404.json, which is internal and returns JSON with content type application/json.
  2. Step 2: Check the return statement

    The return sends status 404 with JSON string '{"error":"Not Found"}' as the body.
  3. Final Answer:

    {"error":"Not Found"} -> Option C
  4. Quick Check:

    Return JSON string = {"error":"Not Found"} [OK]
Hint: Look at return content for JSON body [OK]
Common Mistakes:
  • Assuming default HTML error page is returned
  • Ignoring internal directive effect
  • Confusing status code text with response body
4. You configured nginx to return JSON errors but clients receive HTML error pages instead. What is the likely mistake?
medium
A. The server_name directive is not set.
B. The error_page directive is missing or incorrect.
C. The return directive uses invalid JSON syntax.
D. The internal directive is missing in the error location block.

Solution

  1. Step 1: Understand internal directive role

    The internal directive prevents direct client access to error pages, ensuring nginx uses them only for error responses.
  2. Step 2: Identify why HTML is returned

    If internal is missing, clients can access the error page URL directly, causing nginx to serve default HTML instead of JSON error.
  3. Final Answer:

    The internal directive is missing in the error location block. -> Option D
  4. Quick Check:

    Missing internal = HTML errors shown [OK]
Hint: Always mark error locations internal for JSON errors [OK]
Common Mistakes:
  • Forgetting internal causes direct access to error pages
  • Blaming server_name for error response format
  • Assuming JSON syntax error causes HTML output
5. How would you configure nginx to return a JSON error response with status 500 and body {"error":"Server Error"} using an internal location?
hard
A. error_page 500 /500.json; location = /500.json { internal; default_type application/json; return 500 '{"error":"Server Error"}'; }
B. error_page 500 /500.html; location = /500.html { internal; default_type text/html; return 500 '

Server Error

'; }
C. location /500.json { return 500 '{"error":"Server Error"}'; }
D. error_page 500 /500.json; location /500.json { default_type application/json; return 500 '{"error":"Server Error"}'; }

Solution

  1. Step 1: Use error_page to map 500 to JSON location

    The directive error_page 500 /500.json; tells nginx to use /500.json for 500 errors.
  2. Step 2: Define internal location with JSON content type and return JSON body

    The location must be internal to prevent direct access, set default_type application/json;, and return the JSON string with status 500.
  3. Step 3: Verify options

    error_page 500 /500.json; location = /500.json { internal; default_type application/json; return 500 '{"error":"Server Error"}'; } matches all requirements. error_page 500 /500.html; location = /500.html { internal; default_type text/html; return 500 '

    Server Error

    '; }
    uses HTML. The others lack the internal directive or miss the error_page directive.
  4. Final Answer:

    error_page 500 /500.json; location = /500.json { internal; default_type application/json; return 500 '{"error":"Server Error"}'; } -> Option A
  5. Quick Check:

    Use error_page + internal + JSON return [OK]
Hint: Map error_page, mark internal, set JSON content, return JSON [OK]
Common Mistakes:
  • Omitting internal directive
  • Using wrong content type
  • Not escaping JSON properly
  • Not mapping error_page to location
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard