Bird
Raised Fist0
Nginxdevops~20 mins

CORS configuration in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CORS Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the effect of this nginx CORS configuration?
Given this nginx snippet, what will be the value of the Access-Control-Allow-Origin header in the response?
Nginx
location /api/ {
    add_header Access-Control-Allow-Origin "https://example.com";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header Access-Control-Max-Age 1728000;
        add_header Content-Type "text/plain; charset=UTF-8";
        add_header Content-Length 0;
        return 204;
    }
}
AThe response will include 'Access-Control-Allow-Origin: https://example.com' only for OPTIONS requests.
BThe response will never include 'Access-Control-Allow-Origin' header because it is inside an if block.
CThe response will always include 'Access-Control-Allow-Origin: https://example.com' for all requests to /api/.
DThe response will include 'Access-Control-Allow-Origin: *' for all requests.
Attempts:
2 left
💡 Hint
Remember that add_header outside if applies to all requests unless overridden.
❓ Configuration
intermediate
2:00remaining
Which nginx config snippet correctly allows CORS from any origin?
Select the nginx configuration that correctly sets CORS headers to allow any origin to access resources.
Aadd_header "Access-Control-Allow-Origin" "*" always;
Badd_header Access-Control-Allow-Origin "*";
Cadd_header "Access-Control-Allow-Origin" *;
Dadd_header Access-Control-Allow-Origin *;
Attempts:
2 left
💡 Hint
The header name and value must be quoted properly and the 'always' flag ensures headers are added on all response codes.
❓ Troubleshoot
advanced
2:00remaining
Why does this nginx CORS config fail to allow credentials?
This nginx config snippet is intended to allow CORS with credentials, but browsers still block requests. Why?
Nginx
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Credentials "true";
ABecause Access-Control-Allow-Origin cannot be '*' when Access-Control-Allow-Credentials is true.
BBecause add_header directives must be inside a location block to work.
CBecause Access-Control-Allow-Credentials must be set to 'yes' not 'true'.
DBecause the headers must be added only for OPTIONS requests.
Attempts:
2 left
💡 Hint
Check the CORS specification about credentials and allowed origins.
🔀 Workflow
advanced
2:00remaining
What is the correct order to configure CORS in nginx for a REST API?
Arrange these steps in the correct order to properly configure CORS in nginx for a REST API.
A2,1,3,4
B1,2,3,4
C1,3,4,2
D3,4,1,2
Attempts:
2 left
💡 Hint
Think about what headers must be sent always and what response is needed for preflight.
✅ Best Practice
expert
3:00remaining
Which nginx CORS configuration is best practice for security and flexibility?
Choose the nginx configuration snippet that follows best practices for CORS by allowing only specific origins dynamically and supporting credentials.
A
add_header Access-Control-Allow-Origin "https://example.com" always;
add_header Access-Control-Allow-Credentials "false" always;
B
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Credentials "true" always;
C
add_header Access-Control-Allow-Origin "$http_origin" always;
add_header Access-Control-Allow-Credentials "true" always;
D
if ($http_origin ~* (https?://(www\.)?(example\.com|example\.org))) {
    add_header Access-Control-Allow-Origin "$http_origin" always;
    add_header Access-Control-Allow-Credentials "true" always;
}
Attempts:
2 left
💡 Hint
Dynamic origin matching with regex and conditional headers is more secure than wildcard.

Practice

(1/5)
1. What is the main purpose of configuring CORS in an nginx server?
easy
A. To control which websites can access server resources
B. To improve server performance by caching
C. To encrypt data between client and server
D. To block all incoming traffic

Solution

  1. Step 1: Understand CORS purpose

    CORS (Cross-Origin Resource Sharing) controls which external websites can request resources from your server.
  2. Step 2: Identify correct purpose in options

    Only To control which websites can access server resources describes controlling access from other websites, which matches CORS functionality.
  3. Final Answer:

    To control which websites can access server resources -> Option A
  4. Quick Check:

    CORS controls access = D [OK]
Hint: Remember CORS controls cross-site resource sharing [OK]
Common Mistakes:
  • Confusing CORS with encryption or caching
  • Thinking CORS blocks all traffic
  • Assuming CORS improves performance
2. Which of the following is the correct syntax to add a CORS header allowing all origins in an nginx configuration?
easy
A. add_header "Access-Control-Allow-Origin" "*";
B. add_header "Access-Control-Allow-Origin" *;
C. add_header Access-Control-Allow-Origin "*";
D. add_header Access-Control-Allow-Origin *;

Solution

  1. Step 1: Understand nginx add_header syntax

    The header name does not require quotes unless it contains spaces or special characters. The value must be quoted if it is a string like "*".
  2. Step 2: Identify correct quoting

    add_header Access-Control-Allow-Origin "*"; correctly quotes the value and does not quote the header name, which is the recommended syntax.
  3. Final Answer:

    add_header Access-Control-Allow-Origin "*"; -> Option C
  4. Quick Check:

    Proper quoting for header value only = B [OK]
Hint: Quote header value for safety in nginx, header name usually unquoted [OK]
Common Mistakes:
  • Quoting header name unnecessarily
  • Using * without quotes
  • Misplacing semicolons
3. Given this nginx snippet inside a location block:
if ($request_method = 'OPTIONS') {
  add_header Access-Control-Allow-Origin "*";
  add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
  add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
  add_header Access-Control-Max-Age 1728000;
  add_header Content-Type "text/plain charset=UTF-8";
  add_header Content-Length 0;
  return 204;
}
What will be the HTTP status code returned for an OPTIONS request?
medium
A. 200
B. 204
C. 400
D. 500

Solution

  1. Step 1: Analyze the if block for OPTIONS method

    The configuration checks if the request method is OPTIONS, then sets headers and returns status code 204.
  2. Step 2: Identify the return status code

    The line return 204; explicitly sends HTTP status 204 (No Content) for OPTIONS requests.
  3. Final Answer:

    204 -> Option B
  4. Quick Check:

    OPTIONS returns 204 No Content = B [OK]
Hint: Look for return statement inside OPTIONS check [OK]
Common Mistakes:
  • Assuming default 200 OK instead of 204
  • Confusing OPTIONS with GET method
  • Ignoring explicit return codes
4. You added this to your nginx config to allow CORS:
add_header Access-Control-Allow-Origin *;
But browsers still block cross-origin requests. What is the likely problem?
medium
A. The add_header directive is inside a location block without 'always' parameter
B. The header is missing quotes around '*', causing syntax error
C. nginx does not support CORS headers
D. You need to restart the browser to apply changes

Solution

  1. Step 1: Understand add_header behavior in nginx

    By default, add_header only adds headers on successful (2xx) responses. For OPTIONS or error responses, headers may be missing unless 'always' is used.
  2. Step 2: Identify missing 'always' parameter

    Without 'always', CORS headers may not be sent on OPTIONS or error responses, causing browser blocks.
  3. Final Answer:

    The add_header directive is inside a location block without 'always' parameter -> Option A
  4. Quick Check:

    Missing 'always' causes headers to be skipped = A [OK]
Hint: Use 'always' with add_header to send headers on all responses [OK]
Common Mistakes:
  • Not using 'always' with add_header
  • Assuming nginx lacks CORS support
  • Forgetting to reload nginx after config changes
5. You want to configure nginx to allow CORS only from https://example.com and support preflight OPTIONS requests. Which configuration snippet correctly achieves this?
hard
A. add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Methods" "GET, POST"; return 204;
B. add_header Access-Control-Allow-Origin *; if ($request_method = 'OPTIONS') { return 200; }
C. add_header "Access-Control-Allow-Origin" "https://example.com"; add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; add_header "Access-Control-Allow-Headers" "Content-Type"; return 200;
D. add_header "Access-Control-Allow-Origin" "https://example.com" always; if ($request_method = 'OPTIONS') { add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS" always; add_header "Access-Control-Allow-Headers" "Content-Type" always; add_header "Access-Control-Max-Age" 1728000 always; return 204; }

Solution

  1. Step 1: Check origin restriction

    Only add_header "Access-Control-Allow-Origin" "https://example.com" always; if ($request_method = 'OPTIONS') { add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS" always; add_header "Access-Control-Allow-Headers" "Content-Type" always; add_header "Access-Control-Max-Age" 1728000 always; return 204; } restricts origin to "https://example.com" correctly with proper quoting and 'always' parameter.
  2. Step 2: Verify OPTIONS preflight handling

    The configuration handles OPTIONS requests by returning 204 with required CORS headers and 'always' to ensure headers are sent.
  3. Step 3: Eliminate incorrect options

    Snippets using '*' allow all origins instead of restricting to https://example.com; those lacking full OPTIONS handling, 'always' parameter, or returning 200 instead of 204 fail preflight checks.
  4. Final Answer:

    Correctly restricts origin and handles OPTIONS with headers and 204 -> Option D
  5. Quick Check:

    Restrict origin + OPTIONS 204 + always = C [OK]
Hint: Use 'always' and return 204 for OPTIONS with restricted origin [OK]
Common Mistakes:
  • Using wildcard '*' instead of specific origin
  • Not handling OPTIONS preflight properly
  • Missing 'always' causing headers to be skipped
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