doujuncuo9339 2018-12-03 17:41
浏览 47
已采纳

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

I'm using Google AppEngine with PHP7.2 Runtime and i'm facing a problem with CORS.

With GAE standard environment, it is possible to set headers with 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

With GAE flexible environment, it seems that is not possible:

  • No handlers.http_headers variable is usable
  • Sets Headers directly in the PHP code does not work:

.

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

It seems that GAE LB is dropping every header we want to set.

So... how to handle CORS (or other headers) with GAE in flexible environment and PHP7.2 runtime ?

  • 写回答

1条回答 默认 最新

  • dsla94915 2018-12-05 13:28
    关注

    Finally, I was the only one faulty on this "issue". The routing defined in dispatch.yaml was set in a way that I was not requesting the right GAE service.

    Nevertheless, I've learned that it possible to set headers in two manners:

    • Using the code directly:

    ...

    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题