【问题标题】:Enabling CORS on nginx在 nginx 上启用 CORS
【发布时间】:2018-04-25 14:25:10
【问题描述】:

我已按照this 示例在我的 API 子域上启用 CORS,以便我可以从 SwaggerUI 向它发送请求。这是我在该子域上运行 OPTIONS 得到的输出:

curl -i -X OPTIONS http://api.MYDOMAIN.com/v1/data/extraction

HTTP/1.1 204 No Content
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 18 Apr 2018 20:45:52 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-API-Key
Access-Control-Max-Age: 1728000
Content-Type: text/plain charset=UTF-8
Content-Length: 0

我一直想知道为什么在我的文档子域(相同的 DOMAIN.com 服务器)上的 Chrome 中, 仍然给了我这个。谁能建议我应该去哪里?

【问题讨论】:

  • 我认为这不是 add_header 而是 more_set_headers 命令。现在的问题是 SwaggerUI 没有发送 X-API-KEY 标头...
  • 最后一块拼图是 darrenleeweber 在github.com/swagger-api/swagger-ui/issues/1244 讨论中的建议,它需要在“安全”部分中指定。谢谢大家,问题结束了!
  • 考虑将您的发现作为答案发布,而不是在 cmets 部分。
  • 确实可以,已经凌晨2点了,先睡一觉吧:)

标签: nginx cors swagger-ui


【解决方案1】:

我遇到了类似的问题,但无法正常工作。这个设置终于对我有用。 https://gist.github.com/Stanback/7145487 这就是它的样子。我只是设置$cors 'true' 来测试它是否有效。它对我有用。这些都在 /location {...} 区域中

set $cors '';
    if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)') {
            set $cors 'true';
    }

    if ($cors = 'true') {
            add_header 'Access-Control-Allow-Origin' "$http_origin" always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
            # required to be able to read Authorization header in frontend
            #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
    }

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
            # required to be able to read Authorization header in frontend
            #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
            # Tell client that this pre-flight info is valid for 20 days
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
    }

【讨论】:

    【解决方案2】:

    这一切都归结为 NGINX 使用了 more_set_headers 而不是 add_header(它可能一直是 NGINX 启用了此模块),然后使用上述示例之一使其工作。

    【讨论】:

      【解决方案3】:

      您可能希望使用 curl 和 CORS 原始标头测试对 api.MYDOMAIN.com/v1/data/extraction 的调用。

      curl -i -X POST -H "Origin: http://docs.whatever.com" \
       --verbose http://api.MYDOMAIN.com/v1/data/extraction
      

      响应应该带有标题:

      访问控制允许来源:*

      如果该标头没有随响应返回,chrome 将抛出与您看到的一样的错误。

      附:错误中提到了 nginx 以 404 响应,这可能与它有关。

      【讨论】:

      • 它现在确实返回了标题,我为 nginx 添加了错误的命令。
      【解决方案4】:

      问题很可能是Access-Control-Allow-Origin: *Access-Control-Allow-Credentials: true不兼容的 - 如果您想传递凭据,您需要回复Access-Control-Allow-Origin: {value-of-Origin-request-header}Access-Control-Allow-Credentials: true

      【讨论】:

      • 您可能已经注意到,我已经拥有 Access-Control-Allow-Credentials: true 以及 Access-Control-Allow-Headers:X-API-KEY ,所以唯一的关于您的建议的问题仍然存在 - 如果我指定 Access-Control-Allow-Origin: *?
      • 基本上,检索 Origin 请求标头的值(这将是请求资源的页面的 scheme://domain:port)并将其“镜像”回 ACAO 响应标头.
      猜你喜欢
      • 2019-06-27
      • 2016-09-17
      • 2023-03-24
      • 2018-06-18
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多