0
0
Nginxdevops~20 mins

JSON error responses in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 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.