原文链接:https://juejin.cn/post/7137272408850825246
反代后遇到跨域问题,然后在网上找解决跨域问题,但是总是没有用。
根本上网络给出的解决方案是正常搭建网站情况下跨域,下面的代码添加到反向代理配置文件中即可。
set $enable_cors 0;
if ( $enable_cors = 0 )
{
#CORS 配置
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
#是否允许cookie传输
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token';
}
#针对浏览器的options预请求直接返回200,否则会被403 forbidden--invalie CORS request
if ( $request_method = 'OPTIONS' ) {
#CORS 配置
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
#是否允许cookie传输
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token';
return 200;
}