Nginx配置跨域



Nginx 配置跨域

  • location下配置
location / {
  add_header 'Access-Control-Allow-Origin' $http_origin;
  add_header 'Access-Control-Allow-Credentials' 'true';
  add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
  add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';

  #如果预检请求则返回成功,不需要转发到后端
  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 200;
  }
}

posted @ 2022-06-27 16:45  明知山有小脑斧  阅读(245)  评论(0)    收藏  举报