I'd still like a real solution to this, but for now, I have found this and I'm using the following solution:
location / {
if ($request_method = OPTIONS ) {
add_header Content-Length 0;
add_header Content-Type text/plain;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
add_header Access-Control-Allow-Methods 'GET, POST';
return 200;
}
}
I'm allowing all CORS requests this being my localhost, but beware that this exists for your own security and hence don't be so liberal with Access-Control-Allow-Origin
.
Additionally, I'm using angularjs, and the POST
requests were getting cancelled even after the OPTIONS
request got cleared with the proper permissions. Don't know what's the point of the whole preflight thing. Anyway, additng the Access-Control lines outside the location block fixed that. Like this:
location / {
try_files $uri $uri/ /index.php?$query_string;
if ($request_method = OPTIONS ) {
add_header Content-Length 0;
add_header Content-Type text/plain;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
add_header Access-Control-Allow-Methods 'GET, POST';
return 200;
}
}
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
add_header Access-Control-Allow-Methods 'GET, POST';