I've got two domains:
domain.com sub.domain.com
domain.com needs to make an ajax request to sub.domain.com. I realize that the browser will block this if the request is hardcoded to be sub.domain.com. I tried the following nginx conf:
server { server_name domain.com; rewrite ^/api/(.*)$ http://sub.domain.com/api/$1; }
However, I still get the following error in the browser (Chrome):
No 'Access-Control-Allow-Origin' header is present on the requested resource.
How can I set up nginx to instruct the browser to allow cross domain requests between domain.com and sub.domain.com?
Thanks!
Yes you have to enable it. You have to send CORS allow headers from server side to your browser. This is because a subdomain counts as a different origin.
I think you need to create this inside the location or server block
server { server_name example.com; add_header Access-Control-Allow-Origin sub.example.com; # < this is the needed header # rest of the configuration }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With