Last active
April 26, 2016 02:19
-
-
Save koba04/258636952be7e0c064f34ce2f82f3e36 to your computer and use it in GitHub Desktop.
nginx.conf for CORS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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