CORS stands for Cross-Origin Resource Sharing. It allows a web page from one domain to request resources from another domain safely, preventing security risks.
Click to reveal answer
beginner
Which HTTP header is used to allow cross-origin requests in nginx?
The header Access-Control-Allow-Origin is used to specify which domains can access resources from the server.
Click to reveal answer
beginner
How do you enable CORS for all domains in nginx?
Add the line add_header Access-Control-Allow-Origin "*"; inside the server or location block to allow all domains.
Click to reveal answer
intermediate
What is the purpose of the Access-Control-Allow-Methods header?
It specifies which HTTP methods (GET, POST, etc.) are allowed when accessing the resource from another origin.
Click to reveal answer
intermediate
Why might you need to add Access-Control-Allow-Headers in your nginx CORS setup?
This header tells the browser which custom headers can be sent with the request, enabling more complex requests like those with authentication tokens.
Click to reveal answer
Which nginx directive adds a header to enable CORS?
Aadd_header
Bset_header
Cproxy_pass
Dlisten
✗ Incorrect
The add_header directive is used in nginx to add HTTP headers like those needed for CORS.
What value allows all domains to access your resource via CORS?
A"all"
B"*"
C"none"
D"localhost"
✗ Incorrect
Using "*" in Access-Control-Allow-Origin allows any domain to access the resource.
Which header controls which HTTP methods are allowed in CORS?
AAccess-Control-Allow-Methods
BAccess-Control-Allow-Origin
CAccess-Control-Allow-Headers
DAccess-Control-Expose-Headers
✗ Incorrect
Access-Control-Allow-Methods specifies allowed HTTP methods like GET, POST, PUT.
If you want to allow credentials (cookies) in CORS, which header must NOT be set to "*"?
AAccess-Control-Max-Age
BAccess-Control-Allow-Methods
CAccess-Control-Allow-Headers
DAccess-Control-Allow-Origin
✗ Incorrect
When allowing credentials, Access-Control-Allow-Origin must specify an explicit domain, not "*".
Where in nginx config do you usually place CORS headers?
AOnly in events block
BInside http block only
CInside server or location block
DIn upstream block
✗ Incorrect
CORS headers are typically added inside server or location blocks to control specific resources.
Explain how to configure nginx to allow cross-origin requests from a specific domain.
Think about which header controls allowed origins.
You got /3 concepts.
Describe why CORS is necessary and what problem it solves in web browsers.
Consider how browsers protect users from unsafe data sharing.
You got /3 concepts.
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
Step 1: Understand CORS purpose
CORS (Cross-Origin Resource Sharing) controls which external websites can request resources from your server.
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.
Final Answer:
To control which websites can access server resources -> Option A
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
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 "*".
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.
Final Answer:
add_header Access-Control-Allow-Origin "*"; -> Option C
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:
What will be the HTTP status code returned for an OPTIONS request?
medium
A. 200
B. 204
C. 400
D. 500
Solution
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.
Step 2: Identify the return status code
The line return 204; explicitly sends HTTP status 204 (No Content) for OPTIONS requests.
Final Answer:
204 -> Option B
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
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.
Step 2: Identify missing 'always' parameter
Without 'always', CORS headers may not be sent on OPTIONS or error responses, causing browser blocks.
Final Answer:
The add_header directive is inside a location block without 'always' parameter -> Option A
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
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.
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.
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.
Final Answer:
Correctly restricts origin and handles OPTIONS with headers and 204 -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.