我有两个域:
domain.comsub.domain.com
domain.com需要向sub.domain.com发出ajax请求。我意识到如果请求被硬编码为sub.domaincom,浏览器会阻止它。我尝试了以下nginx-conf:
server {
server_name domain.com;
rewrite ^/api/(.*)$ http://sub.domain.com/api/$1; }
然而,我仍然在浏览器(Chrome)中收到以下错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
如何设置nginx来指示浏览器允许domain.com和sub.domain.com之间的跨域请求?
谢谢!
我认为您需要在位置或服务器块中创建
server {
server_name example.com;
add_header Access-Control-Allow-Origin sub.example.com; # < this is the needed header
# rest of the configuration
}