设置nginx允许子域跨域请求 [英] Set up nginx to allow cross-domain request for subdomain
本文介绍了设置nginx允许子域跨域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个域:
domain.com sub.domain.com
domain.com需要向sub.domain.com发出Ajax请求。我意识到,如果请求被硬编码为sub.domain.com,浏览器就会发挡路。我尝试了以下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
}
这篇关于设置nginx允许子域跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文