无法让 CORS 在 Google Endpoints ESP 上运行

Can't get CORS working on a Google Endpoints ESP

我是 运行 K8s 集群,使用企业服务代理提供 gRPC 服务,通过 HTTPS 提供 gRPC 和 REST。

我正在尝试为此终结点启用 CORS,但我无法使其正常工作。

这是我的端点配置:

...
endpoints:
- name: <my-service>.endpoints.<my-project>.cloud.goog
  target: <ENDPOINT_IP>
  allow_cors: true

我更新了端点并更新了 ESP 配置。我已经确认 /etc/nginx.d/service.json 中有一个部分或包含有关启用 CORS 的内容。

但是,当我向端点发出 OPTIONS 请求时,我收到错误消息 The service does not allow CORS traffic.

我没主意了。有谁知道如何为我的端点启用 CORS?

不幸的是,allow_cors 意味着 ESP 只会将 OPTIONS 请求传递给后端。而 gRPC 服务器不支持 OPTIONS 请求,所以它不会工作。有解决方法,请参阅 https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/google-cloud-endpoints/THvCfetfzW8/luyH3tOUBgAJ

记得使用 --cors_preset=basic

在 ESP 上启用 CORS

示例:

    - name: esp
      image: gcr.io/endpoints-release/endpoints-runtime:1
      args: [
        "--http_port=8081",
        "--backend=127.0.0.1:8080",
        "--service=...",
        "--rollout_strategy=managed",
        "--cors_preset=basic",
      ]

查看 Google 文档:https://cloud.google.com/endpoints/docs/openapi/specify-proxy-startup-options#adding_cors_support_to_esp