首页 > 解决方案 > 当 OPTIONS 方法不可缓存时,为什么要在 Access-Control-Request-Method、ACRH 和 origin 上添加 Vary?

问题描述

我发现了 CORS 的 go implementation,它在每个 CORS 请求中添加了以下标头:

headers.Add("Vary", "Origin")
headers.Add("Vary", "Access-Control-Request-Method")
headers.Add("Vary", "Access-Control-Request-Headers")

https://github.com/rs/cors/blob/f9bce55a4e61e3d1a061993e3453eb9848fcdc4d/cors.go#L269

但据我所知 OPTIONS 方法不可缓存?

那么他们为什么要添加 Vary 标头呢?

标签: httpcors

解决方案


OPTIONS 方法可以被中间缓存缓存。

例如:

If you want OPTIONS responses to be cached, do the following:(...)

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html


推荐阅读