在使用 AWS AMI 时,我在 3scale 中遇到了同样的问题,并且能够通过将 app_key 和 app_id 添加到选项请求的允许标头来解决它。
在测试请求在邮递员中工作但不能通过 Chrome 工作。在我的情况下,当浏览器发出预检选项检查时,它会导致拒绝,因为 CORS 默认值不允许 app_key 和 app_id 标头。
添加对这些标头的支持可以通过在“Access-Control-Allow-Headers”标头的末尾添加一个条目来实现。我将此配置设置为一个名为cors.conf的单独文件:
#### CORS ####
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,app_id,app_key';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
#### CORS END ####
然后将 cors.conf 包含在nginx.conf的任何“location /”块下:
...
location / {
include /opt/openresty/nginx/conf/cors.conf;
set $provider_key null;
set $cached_key null;
...
进行此更改后,我的浏览器就能够成功发出请求。
Nginx 文档上的CORS被用作基线,我注意到只有 OPTIONS 部分需要修改以获得所需的结果。