宝塔解决同源策略阻挡
-
访问宝塔面板(如
http://你的服务器IP:端口号
) -
进入「网站」模块 → 在列表中找到目标网站
-
点击「配置文件」 → 选择「配置文件」
-
增加以下规则,二选一,选适合的即可
1、只允许特定的请求来源,例如www.bing.com
set $dom 'no';
if ($http_origin ~* (https?://www\.bing\.com(:[0-9]+)?$)) {set $dom $http_origin;
}
add_header 'Access-Control-Allow-Origin' $dom;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length, Content-Range';
2、允许任何来源的跨域请求
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length, Content-Range';