Skip to content

Instantly share code, notes, and snippets.

@pimentelra
Forked from Stanback/nginx.conf
Last active July 17, 2019 13:24
Show Gist options
  • Save pimentelra/f5722c0e88b7cc02846705fbb96b41db to your computer and use it in GitHub Desktop.
Save pimentelra/f5722c0e88b7cc02846705fbb96b41db to your computer and use it in GitHub Desktop.
Example Nginx configuration for adding cross-origin resource sharing (CORS) support to reverse proxied APIs
map $http_origin $cors_origin_header {
default "";
"~(^|^http:\/\/)(localhost$|localhost:[0-9]{1,4}$)" "$http_origin";
"~^https://test-.-dev.example.pl$" "$http_origin"; # https://test-7-dev.example.pl
"https://test.example.com" "$http_origin";
}
map $http_origin $cors_cred {
default "";
"~(^|^http:\/\/)(localhost$|localhost:[0-9]{1,4}$)" "true";
"~^https://test-.-dev.example.pl$" "true"; # https://test-7-dev.example.pl
"https://test.example.com" "true";
}
server {
listen 443 ssl http2;
server_name api.example.com;
include ssl/wildcard;
add_header Access-Control-Allow-Origin $cors_origin_header always;
add_header Access-Control-Allow-Credentials $cors_cred;
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
if ($request_method = 'OPTIONS' ) {
return 204 no-content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment