Rails API on nginx + React frontend returning The Same Origin Policy disallows reading the remote resource
I am building an API-only Rails app running on nginx, and have a react frontend on another domain, whenever I attempt to make a request to the api server from react, fetch & axios both return The Same Origin Policy disallows reading the remote resource. When I use curl or postman to test the API, they both work, and I receive the appropriate headers. I have tried using rack-cors but it doesn't insert the headers when behind a proxy, so I have my virtual host for the API configured like so:
# Thin Ruby server
upstream rails_app {
server 127.0.0.1:3000;
}
server {
listen 80;
listen 443 ssl;
server_name api.myapp.com;
ssl_certificate /myapp-secure/myapp.crt;
ssl_certificate_key /myapp-secure/myapp.key;
access_log /myapp-api/log/access.log;
error_log /myapp-api/log/error.log;
root /myapp-api/public/;
index index.html;
add_header 'Access-Control-Max-Age' 17280000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Request-Method' '*';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Authorization' always;
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://rails_app;
}
}
And I have a basic login function on the frontend to test a working api endpoint (Should just return an error that no username/password was supplied):
login() {
axios.post('https://api.myapp.com/v1/login', {
crossdomain: true
})
.then(res => res.json)
.then(res => console.log(res))
.catch(error => console.log(error))
}
I have the webpack-dev-server headers configured like so:
headers:
'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
'Access-Control-Allow-Headers': '*'
I'm not sure what I'm doing wrong and I'm wasting far too much time trying to overcome this issue, any help would be greatly appreciated!
ruby-on-rails nginx
add a comment |
I am building an API-only Rails app running on nginx, and have a react frontend on another domain, whenever I attempt to make a request to the api server from react, fetch & axios both return The Same Origin Policy disallows reading the remote resource. When I use curl or postman to test the API, they both work, and I receive the appropriate headers. I have tried using rack-cors but it doesn't insert the headers when behind a proxy, so I have my virtual host for the API configured like so:
# Thin Ruby server
upstream rails_app {
server 127.0.0.1:3000;
}
server {
listen 80;
listen 443 ssl;
server_name api.myapp.com;
ssl_certificate /myapp-secure/myapp.crt;
ssl_certificate_key /myapp-secure/myapp.key;
access_log /myapp-api/log/access.log;
error_log /myapp-api/log/error.log;
root /myapp-api/public/;
index index.html;
add_header 'Access-Control-Max-Age' 17280000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Request-Method' '*';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Authorization' always;
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://rails_app;
}
}
And I have a basic login function on the frontend to test a working api endpoint (Should just return an error that no username/password was supplied):
login() {
axios.post('https://api.myapp.com/v1/login', {
crossdomain: true
})
.then(res => res.json)
.then(res => console.log(res))
.catch(error => console.log(error))
}
I have the webpack-dev-server headers configured like so:
headers:
'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
'Access-Control-Allow-Headers': '*'
I'm not sure what I'm doing wrong and I'm wasting far too much time trying to overcome this issue, any help would be greatly appreciated!
ruby-on-rails nginx
1
Chrome is the app that is disallowing CORS. If Chrome doesn't see theAccess-Control-Allow-Origin: *then it will stop the JavaScript.curlwill always work, because it doesn't enforce header policies. Usecurl -ito verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.
– Chloe
Nov 18 '18 at 23:08
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22
add a comment |
I am building an API-only Rails app running on nginx, and have a react frontend on another domain, whenever I attempt to make a request to the api server from react, fetch & axios both return The Same Origin Policy disallows reading the remote resource. When I use curl or postman to test the API, they both work, and I receive the appropriate headers. I have tried using rack-cors but it doesn't insert the headers when behind a proxy, so I have my virtual host for the API configured like so:
# Thin Ruby server
upstream rails_app {
server 127.0.0.1:3000;
}
server {
listen 80;
listen 443 ssl;
server_name api.myapp.com;
ssl_certificate /myapp-secure/myapp.crt;
ssl_certificate_key /myapp-secure/myapp.key;
access_log /myapp-api/log/access.log;
error_log /myapp-api/log/error.log;
root /myapp-api/public/;
index index.html;
add_header 'Access-Control-Max-Age' 17280000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Request-Method' '*';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Authorization' always;
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://rails_app;
}
}
And I have a basic login function on the frontend to test a working api endpoint (Should just return an error that no username/password was supplied):
login() {
axios.post('https://api.myapp.com/v1/login', {
crossdomain: true
})
.then(res => res.json)
.then(res => console.log(res))
.catch(error => console.log(error))
}
I have the webpack-dev-server headers configured like so:
headers:
'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
'Access-Control-Allow-Headers': '*'
I'm not sure what I'm doing wrong and I'm wasting far too much time trying to overcome this issue, any help would be greatly appreciated!
ruby-on-rails nginx
I am building an API-only Rails app running on nginx, and have a react frontend on another domain, whenever I attempt to make a request to the api server from react, fetch & axios both return The Same Origin Policy disallows reading the remote resource. When I use curl or postman to test the API, they both work, and I receive the appropriate headers. I have tried using rack-cors but it doesn't insert the headers when behind a proxy, so I have my virtual host for the API configured like so:
# Thin Ruby server
upstream rails_app {
server 127.0.0.1:3000;
}
server {
listen 80;
listen 443 ssl;
server_name api.myapp.com;
ssl_certificate /myapp-secure/myapp.crt;
ssl_certificate_key /myapp-secure/myapp.key;
access_log /myapp-api/log/access.log;
error_log /myapp-api/log/error.log;
root /myapp-api/public/;
index index.html;
add_header 'Access-Control-Max-Age' 17280000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Request-Method' '*';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Expose-Headers' 'Authorization' always;
location / {
try_files $uri @rails;
}
location @rails {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://rails_app;
}
}
And I have a basic login function on the frontend to test a working api endpoint (Should just return an error that no username/password was supplied):
login() {
axios.post('https://api.myapp.com/v1/login', {
crossdomain: true
})
.then(res => res.json)
.then(res => console.log(res))
.catch(error => console.log(error))
}
I have the webpack-dev-server headers configured like so:
headers:
'Access-Control-Allow-Origin': '*'
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
'Access-Control-Allow-Headers': '*'
I'm not sure what I'm doing wrong and I'm wasting far too much time trying to overcome this issue, any help would be greatly appreciated!
ruby-on-rails nginx
ruby-on-rails nginx
asked Nov 18 '18 at 22:54
DivXZeroDivXZero
3881515
3881515
1
Chrome is the app that is disallowing CORS. If Chrome doesn't see theAccess-Control-Allow-Origin: *then it will stop the JavaScript.curlwill always work, because it doesn't enforce header policies. Usecurl -ito verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.
– Chloe
Nov 18 '18 at 23:08
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22
add a comment |
1
Chrome is the app that is disallowing CORS. If Chrome doesn't see theAccess-Control-Allow-Origin: *then it will stop the JavaScript.curlwill always work, because it doesn't enforce header policies. Usecurl -ito verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.
– Chloe
Nov 18 '18 at 23:08
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22
1
1
Chrome is the app that is disallowing CORS. If Chrome doesn't see the
Access-Control-Allow-Origin: * then it will stop the JavaScript. curl will always work, because it doesn't enforce header policies. Use curl -i to verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.– Chloe
Nov 18 '18 at 23:08
Chrome is the app that is disallowing CORS. If Chrome doesn't see the
Access-Control-Allow-Origin: * then it will stop the JavaScript. curl will always work, because it doesn't enforce header policies. Use curl -i to verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.– Chloe
Nov 18 '18 at 23:08
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22
add a comment |
1 Answer
1
active
oldest
votes
My issue was two-part, but I was able to work through it and determine what was going on:
- You cannot use
localhost,.dev, or.localdomains when making CORS requests. They will always be denied regardless of what access policy you have set. - Responses from nginx will include the appropriate headers except for the case of an xhr request, in which case only the response headers from the proxied service are returned. (Basically don't set any headers in nginx, have your application handle them. In my case rack-cors did the job but only after I removed all headers from nginx)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366249%2frails-api-on-nginx-react-frontend-returning-the-same-origin-policy-disallows-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
My issue was two-part, but I was able to work through it and determine what was going on:
- You cannot use
localhost,.dev, or.localdomains when making CORS requests. They will always be denied regardless of what access policy you have set. - Responses from nginx will include the appropriate headers except for the case of an xhr request, in which case only the response headers from the proxied service are returned. (Basically don't set any headers in nginx, have your application handle them. In my case rack-cors did the job but only after I removed all headers from nginx)
add a comment |
My issue was two-part, but I was able to work through it and determine what was going on:
- You cannot use
localhost,.dev, or.localdomains when making CORS requests. They will always be denied regardless of what access policy you have set. - Responses from nginx will include the appropriate headers except for the case of an xhr request, in which case only the response headers from the proxied service are returned. (Basically don't set any headers in nginx, have your application handle them. In my case rack-cors did the job but only after I removed all headers from nginx)
add a comment |
My issue was two-part, but I was able to work through it and determine what was going on:
- You cannot use
localhost,.dev, or.localdomains when making CORS requests. They will always be denied regardless of what access policy you have set. - Responses from nginx will include the appropriate headers except for the case of an xhr request, in which case only the response headers from the proxied service are returned. (Basically don't set any headers in nginx, have your application handle them. In my case rack-cors did the job but only after I removed all headers from nginx)
My issue was two-part, but I was able to work through it and determine what was going on:
- You cannot use
localhost,.dev, or.localdomains when making CORS requests. They will always be denied regardless of what access policy you have set. - Responses from nginx will include the appropriate headers except for the case of an xhr request, in which case only the response headers from the proxied service are returned. (Basically don't set any headers in nginx, have your application handle them. In my case rack-cors did the job but only after I removed all headers from nginx)
answered Nov 19 '18 at 19:36
DivXZeroDivXZero
3881515
3881515
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366249%2frails-api-on-nginx-react-frontend-returning-the-same-origin-policy-disallows-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

1
Chrome is the app that is disallowing CORS. If Chrome doesn't see the
Access-Control-Allow-Origin: *then it will stop the JavaScript.curlwill always work, because it doesn't enforce header policies. Usecurl -ito verify your API server is spitting out the right headers. Oh, and all of software development is about wasting too much time trying to figure out something stupid simple.– Chloe
Nov 18 '18 at 23:08
@chloe The 'Access-Control-Allow-Origin': * header is being returned from both the API as well as the frontend webserver, the issue persists.
– DivXZero
Nov 19 '18 at 0:22