Skip to content

Instantly share code, notes, and snippets.

@koba04
Last active April 26, 2016 02:19
Show Gist options
  • Save koba04/258636952be7e0c064f34ce2f82f3e36 to your computer and use it in GitHub Desktop.
Save koba04/258636952be7e0c064f34ce2f82f3e36 to your computer and use it in GitHub Desktop.
nginx.conf for CORS
location / {
if ($http_origin = 'http://api.example.com') {
set $cors 'true';
}
if ($request_method = 'OPTIONS') {
set $cors "${cors}:options";
}
if ($request_method ~ ^(POST|GET)$) {
set $cors "${cors}:allow_methods";
}
if ($cors = 'true:allow_methods') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
}
if ($cors = 'true:options') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Max-Age' 86400;
return 204;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment