繁体   English   中英

如何在Flex环境和PHP运行时中使用GAE处理CORS(或任何标头)?

[英]How to handle CORS (or whatever headers) with GAE in flex environment and PHP runtime?

我将Google AppEngine与PHP7.2 Runtime一起使用,并且遇到了CORS问题。

在GAE 标准环境下,可以使用app.yaml(handlers.http_headers)cf( https://cloud.google.com/appengine/docs/standard/php/config/appref#handlers_element )设置标头:

handlers:
- url: /images
  static_dir: static/images
  http_headers:
    X-Foo-Header: foo
    X-Bar-Header: bar value

借助GAE 灵活的环境,似乎不可能:

  • 没有handlers.http_headers变量可用
  • 直接在PHP代码中设置标头无效:

$response->headers->add(
    [
        'Access-Control-Allow-Origin' => 'http:/blabla-dot-my-app.appspot.com',
        'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
    ]
);

看来GAE LB正在删除我们要设置的每个标头。

那么...如何在灵活的环境和PHP7.2运行时中使用GAE处理CORS(或其他标头)?

最后,我是这个“问题”上唯一的一个错误。 dispatch.yaml中定义的路由设置是因为我没有请求正确的GAE 服务

不过,我了解到可以通过两种方式设置标题:

  • 直接使用代码:

...

$response->headers->add(
    [
        'Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Methods' => 'GET, POST',
        'Access-Control-Allow-Headers' => 'Content-Type,Authorization',
    ]
);

...

# CORS
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';

location / {
  # CORS (again)
  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    #
    # 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;
  }
  # try to serve files directly, fallback to the front controller
  try_files $uri /$front_controller_file$is_args$args;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM