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 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架