0
0
Nginxdevops~20 mins

CORS configuration in Nginx - Practice Problems & Coding Challenges

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