Skip to content

Instantly share code, notes, and snippets.

@haohcraft
Forked from Stanback/nginx.conf
Last active November 5, 2015 15:20
Show Gist options
  • Save haohcraft/cc6744b95cb414bfbd6c to your computer and use it in GitHub Desktop.
Save haohcraft/cc6744b95cb414bfbd6c to your computer and use it in GitHub Desktop.
Example Nginx configuration for adding cross-origin resource sharing (CORS) support to reverse proxied APIs
#
# CORS header support
#
# One way to use this is by placing it into a file called "cors_support"
# under your Nginx configuration directory and placing the following
# statement inside your location block(s):
#
# include cors_support;
#
# A limitation to this method is that Nginx doesn't currently send headers
# specified by add_header when the backend returns a 4xx or 5xx status code.
#
# For more information on CORS, please see: http://enable-cors.org/
# Forked from this Gist: https://gist.github.com/michiel/1064640
#
set $cors '';
if ($http_origin ~* (https?://(localhost|[^/]*\.domain\.com(:[0-9]+)?)$)) {
set $cors 'true';
}
if ($cors = 'true') {
more_set_headers 'Access-Control-Allow-Origin' "$http_origin";
more_set_headers 'Access-Control-Allow-Credentials' 'true';
more_set_headers 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
more_set_headers 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
}
if ($request_method = 'OPTIONS') {
return 204;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment