Skip to content

简单请求

必须满足下列所有条件

  • 请求方法是 GET POST HEAD(只请求资源首部)
  • 首部只能包含下面字段
    • accept
    • accept-language
    • content-language
    • content-type
  • content-type 的值只能是
    • application/x-www-form-urlencode
    • multipart/form-data
    • text/plain

复杂请求

不满足简单请求的都是复杂请求, 常见的复杂请求包括 content-type: applcation/json

cors cross-origin resource sharing 跨域资源共享

简单请求

针对简单请求,浏览器发现是跨域,会在请求中添加origin字段,如果服务端允许跨域,会返回access-control-allow-origin 字段,浏览器检测返回字段是否和当前域匹配,决定是否舍弃当前请求。

响应会返回下面3个首部字段

  • access-control-allow-origin, 指定允许的域。只能是origin 字段或者*
  • access-control-allow-credentials,决定下次请求是否可以携带cookies
  • access-control-allow-headers, 允许浏览器额外获取的首部字段

复杂请求

针对复杂请求浏览器会先发送预检请求(options),检查当前请求能否跨域,允许的字段等。 浏览器发送预检查请求,包括Origin,access-control-request-method(表示当前请求的方法),access-control-allow-Headers(表示当前请求额外的首部字段)

服务端根据预检请求,会返回origin,access-control-allow-methods(表示允许的方法),用逗号分隔access-control-allow-headers(表示允许的首部字段)(逗号分隔),浏览器会检查,如果和当前匹配,则会发送正式请求。

为了避免浏览器每次需要预检请求,还会同时返回access-control-max-age(秒), 表示有效期内不用发送预检请求。如果一个请求背、被预检通过,会走简单请求的流程,如果这个时候被简单请求拦截, 也不会成功

nginx 配置cors

nginx
location / {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'POST,DELETE,OPTIONS';
    add_header Access-Control-Max-Age: 7200
    add_header Aceess-Contorl-Allow-Headers 'Cache-Control,Keep-Alive,If-Modified-Since,Content-Type,Authorization'
}

或者单独对options 请求配置

nginx
location / {
    if($request_method = 'OPTIONS) {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS';
        add_header Access-Control-Allow-Header 'Cache-control,If-Modified-Since,If-None-Matched,Authorization,Content-Type'
    }
}