Skip to content

Instantly share code, notes, and snippets.

@amessinger
Created February 28, 2017 16:37
Show Gist options
  • Save amessinger/6b365c9b0fdd9c065e62b2d98c64fa0c to your computer and use it in GitHub Desktop.
Save amessinger/6b365c9b0fdd9c065e62b2d98c64fa0c to your computer and use it in GitHub Desktop.
Proxy Cors Config for Nginx
upstream service {
server {{ ip }}:{{ port }};
}
server {
if ($http_origin ~* (https?://localhost(:[0-9]+)?)) {
rewrite ^ /__cors__/$request_method$uri last;
}
location /__cors__/OPTIONS/ {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,X-Client-Id';
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 204;
}
location /__cors__/ {
add_header 'Access-Control-Allow-Origin' "$http_origin";
rewrite ^/__cors__/(GET|POST)/(.*)$ /$2 break;
proxy_pass http://service;
}
location / {
proxy_pass http://service;
}
}
@lisael
Copy link

lisael commented Mar 1, 2017

@xcambar c'est surment pas idéal, mais on peut mettre ixaekahjin3tieth3eafeiChe5ahnumiCein5Go0ru (et le changer 4 fois par jour si on est vraiment flippés) à la place de __cors__, comme ce ne sont que des rewrite internes, cette partie d'URL ne leak pas à l'exterieur de nginx.

Pour ce qui est des endpoints privés, le but va etre justement d'intégrer ca avec https://github.com/peopledoc/api-gateway/blob/configure_nginx/ansible/roles/gateway.nginx/templates/api.site

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment