📖 Scenario: You are setting up a web server using Nginx. You want to make sure that when users encounter errors like 404 or 500, they get a clear message in JSON format instead of the default HTML error page.
🎯 Goal: Configure Nginx to return JSON formatted error responses for HTTP status codes 404 and 500.
📋 What You'll Learn
Create a basic Nginx server block configuration
Add a variable to hold the JSON error message
Use the error_page directive to handle 404 and 500 errors
Return the JSON error message with the correct content type
Test the configuration by simulating 404 and 500 errors
💡 Why This Matters
🌍 Real World
Web servers often need to return error messages in JSON format for APIs or modern web apps that expect JSON responses instead of HTML error pages.
💼 Career
Knowing how to customize error responses in Nginx is a valuable skill for DevOps engineers and backend developers managing web infrastructure.
Progress0 / 4 steps
1
Create a basic Nginx server block
Create a basic Nginx server block listening on port 80 with the server name localhost. Inside the server block, add a location / block that returns a 200 status with the text "Welcome to Nginx".
Nginx
Hint
Use server {} block with listen 80; and server_name localhost;. Inside location / {}, use return 200 'Welcome to Nginx';.
2
Add a variable for JSON error message
Inside the server block, create a variable called $json_error that holds the JSON string {"error": "Resource not found"}.
Nginx
Hint
Use the set directive to assign the JSON string to $json_error.
3
Configure error_page for 404 and 500 errors
Add error_page directives inside the server block to handle HTTP status codes 404 and 500. For both errors, redirect to /json_error location.
Nginx
Hint
Use error_page 404 /json_error; and error_page 500 /json_error; inside the server block.
4
Create /json_error location to return JSON error
Add a location /json_error block inside the server block. Inside it, set the Content-Type header to application/json and return the $json_error variable with status 200.
Nginx
Hint
Use location /json_error {} with default_type application/json; and return 200 $json_error;.
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