Skip to content

Instantly share code, notes, and snippets.

@ThomasLau
Last active August 29, 2015 14:18
Show Gist options
  • Save ThomasLau/41e915fea0089226e733 to your computer and use it in GitHub Desktop.
Save ThomasLau/41e915fea0089226e733 to your computer and use it in GitHub Desktop.
CORS on Nginx

The following Nginx configuration enables CORS, with support for preflight requests, using a regular expression to define a whitelist of allowed origins, and various default values that may be needed to workaround incorrect browser implementations.

map $http_origin $cors_header {
    default "";
    "~^https?://[^/]+\.example\.com(:[0-9]+)?$" "$http_origin";
}

server {
    ...
    location / {
        add_header Access-Control-Allow-Origin $cors_header;
        try_files $uri $uri/ /index.php;
    }
    ...
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment