nginx 跨域配置

为了给不同域名调用自己的api,那么我这api的网站就要配置允许跨域访问

if ($request_method = "OPTIONS") {
  add_header "Access-Control-Allow-Origin" *;
  add_header "Access-Control-Allow-Methods" "GET,POST,OPTIONS";
  add_header "Access-Control-Allow-Headers" *;
  add_header 'Access-Control-Allow-Credentials' true;
  # add_header "Access-Control-Max-Age" 1728000;
  # add_header "Content-Type" "text/plain charset=UTF-8";
  # add_header "Content-Length" 0;
  return 204;
}
if ($request_method ~* (GET|POST)) {
  add_header "Access-Control-Allow-Origin" *;
  add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
  add_header "Access-Control-Allow-Headers" *;
}

放在location内,想放哪放哪,随便。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注