AngularJS + Ngingx CORS
I am trying to do the following setup:
API
api.domain.com
APP
domain.com
I have installed 2 different nginx configurators, one for each of the two urls. They both work when doing normal GET requests in the browser (api.domain.com/test), but it all falls apart when making a $ resource call from angularjs (POST). This returns
not allowed by Access-Control-Allow-Origin.
I've been doing this for a while and playing around with different config files. I am currently using the following file, but I do not know if this will work.
Any help would be greatly appreciated
source to share
You can refer to this post.
I am new to angularjs too. And I am writing my internal code with nodejs. When trying to use the backend api in frontend with angularjs I met the same problem as you.
So I use nginx to store my application and then in my file /etc/nginx/conf.d/default.conf
I can change the router using nginx and this way the problem caused by Access-Control-Allow-Origin can be avoided.
Hope this helps you.
source to share
To make CORS work in Angular JS try adding this config to your Angular app module
angular.module('simpleRESTapp')
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
For the Nginx side, you need to configure nginx or your application server to return a header Access-Control-Allow-Origin
containing the value of the header Origin
sent by the client if it matches the allowed list. I think this config should work,
if ($http_origin ~ "^(http://developers.example.com|http://example.com)$") {
add_header "Access-Control-Allow-Origin" $http_origin;
}
source to share