amazon-web-services - 如何根据允许的来源列表设置 Access-Control-Allow-Origin header 的值?

标签 amazon-web-services ubuntu nginx amazon-ec2 cors

我已经阅读了很多帖子,但没有一个有效。

我在安装了 ubuntu 16.04 和 nginx 的 aws 中安装了 ec2。进入site-available/site-enabled'Access-Control-Allow-Origin'允许一个域访问,但是如何添加多个域访问?

如果我添加多个访问权限,我会收到多个值错误。
很多帖子都是关于 php 的,例如

NGINX 'Access-Control-Allow-Origin' header contains multiple values

即使我没有使用 php,但我已经尝试使用上面的代码但不起作用。

有没有人可以帮我或指导我如何在服务器上完成?

提前致谢。

最佳答案

您可以有条件地导致 Access-Control-Allow-Origin通过将以下内容添加到您的 nginx 配置中,以正确的值发送响应 header 。

location / {
  set $is_allowed_origin "";
  if ($http_origin = "https://some.allowed.origin") {
    set $is_allowed_origin "true";
  }
  if ($http_origin = "https://another.allowed.origin") {
    set $is_allowed_origin "true";
  }
  if ($is_allowed_origin = "true") {
    add_header "Access-Control-Allow-Origin" "$http_origin";
  }
}

这将导致 Access-Control-Allow-Origin: https://some.allowed.origin如果 Origin 的值则发送请求中的请求头是 https://some.allowed.origin , 并会导致 Access-Control-Allow-Origin: https://another.allowed.origin如果 Origin 则发送是 https://another.allowed.origin , ETC。

如果 Origin 的值请求头既不是 https://some.allowed.originhttps://another.allowed.origin ,然后没有 Access-Control-Allow-Origin将被发送。

关于amazon-web-services - 如何根据允许的来源列表设置 Access-Control-Allow-Origin header 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420983/

相关文章:

python - Django S3 Direct 上的 CORS 错误

python - AWS Elasticsearch 服务角色权限

c - 在 C 中复制无符号整数值时出现段错误

ubuntu - 从 ubuntu 中删除旧构建的 erlang

ssl - 本地注册表上的 docker 推送问题 https 对资源的访问被拒绝

amazon-web-services - 需要从AWS中已安装应用程序的机器拍摄Docker镜像或容器的镜像

amazon-web-services - 如何选择重新通过 Amazon Pinpoint 接收 SMS 消息

python - 属性错误 : 'NoneType' object has no attribute 'loader'

nginx - 如何重新启动由乘客安装的 nginx

nginx - uwsgicluster - 连接到上游客户端时没有实时上游