server {
#其他配置.....
location / {
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, OPTIONS, DELETE';
add_header Access-Control-Expose-Headers '*';
add_header Access-Control-Allow-Headers 'Origin, Content-Type, Cookie,X-CSRF-TOKEN,Accept,Authorization,sign,timestamps,ticket,token-platform,x-requested-with,usertoken,userkey';
if ($request_method = 'OPTIONS') {
return 200;
}
#其他配置.....
}
注意点:
1,add_header Access-Control-Allow-Headers 这个配置请求header,所有自定义的header这里面必须全部包含;
2,if ($request_method = 'OPTIONS') 判断语句必须加上,校验前端options请求,直接返回200即可;