今天我遇到了个跨域的问题,之前跨域都是后端人员代码中允许跨域的,其实还可以使用nginx的反向代理来允许跨域,搜索了下网上资料,结果发现基本上全都是错的,也不知道这些人有没有自己敲过试试,无语
正确做法:
重点在于 always关键字;
例如:add_header ‘’ ‘’ always;
location /api{
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
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' always;
if ( $request_method = 'OPTIONS' ) {
return 200;
}
proxy_pass http://192.168.1.55:8888/api;
}