【问题标题】:Enabling CORS on Google App Engine Flexible environment PHP (Laravel)在 Google App Engine 柔性环境 PHP (Laravel) 上启用 CORS
【发布时间】:2021-01-29 18:12:01
【问题描述】:

0

我在为使用 Google App Engine 和柔性环境托管的 PHP (laravel) 应用程序启用 CORS 支持时遇到问题。

使用 axios 库的每个 AJAX 请求都会导致以下错误...

从源“http://localhost:8080”访问“https://api.[something].services/request”处的 XMLHttpRequest 已被 CORS 策略阻止:否“Access-Control-Allow-Origin”请求的资源上存在标头。

【问题讨论】:

    标签: php google-app-engine


    【解决方案1】:

    您很可能需要添加:

    header('Access-Control-Allow-Origin: *');
    

    上面的 PHP 代码表明通配符 * 将允许 每个人 向您的 Web 应用程序发出 Ajax 请求。

    header('Access-Control-Allow-Origin: http://example.com');
    

    上面的 PHP 代码表明example.com 有权向您的 Web 应用程序发出跨域请求。

    另外,我建议你看看这个PHP Laravel package 发送带有 Laravel 中间件配置的跨域资源共享标头。

    另见:

    【讨论】:

    • 我尝试使用这个 PHP Laravel 包并在 index.php 中添加标头,但无法发送请求 ajax。
    • 我在其他服务器 linux 中使用 github.com/fruitcake/laravel-cors 可以正常工作。但是 GAE flex 的 deloy 它不起作用
    • 我使用了 nginx-app.conf 但遇到了同样的问题。你能帮我吗
    • 我明白了,我认为如果您可以发布一个最小的可重现示例代码来查看您的配置/您如何提出请求并能够进一步帮助您,那就太好了。您的其他 Linux 服务器托管在哪里?还有 GCP?
    【解决方案2】:
    runtime: php
    env: flex
    manual_scaling:
      instances: 1
    resources:
      cpu: 4
      memory_gb: 4
      disk_size_gb: 20
    runtime_config:
      document_root: public
    
    # Ensure we skip ".env", which is only for local development
    skip_files:
      - .env
      - .git
      - /vendor/
      - /node_modules/
    
    env_variables:
      # Put production environment variables here.
      APP_LOG: errorlog
      APP_KEY: [REDACTED]
      APP_NAME: MyApp
      APP_ENV: production
      APP_DEBUG: false
      APP_URL: https://example.com
      CUSTOM_CSS: example.css
      FRONTEND_URL: https://web.example.com
    
      LOG_CHANNEL: stackdriver
      LOG_SLACK_WEBHOOK_URL: https://hooks.slack.com/services/T011E768GNR/B011E7A307P/CGK4zZwTG1tieLwZ1uTx77
    
      DB_CONNECTION: mysql
      DB_HOST: 127.0.0.1
      DB_PORT: 3306
      DB_DATABASE: dbname
      DB_USERNAME: root
      DB_PASSWORD: [REDACTED]
      DB_SOCKET: "[REDACTED]"
    

    这是 app.yml 的一部分

    This is a part of composer.json

    protected $middleware = [
            \App\Http\Middleware\TrustProxies::class,
            \App\Http\Middleware\CheckForMaintenanceMode::class,
            \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
            \App\Http\Middleware\TrimStrings::class,
            \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
            \App\Http\Middleware\GaeProxyIp::class,
            \App\Http\Middleware\GaeSetHstsHeader::class,
            \Fruitcake\Cors\HandleCors::class,
        ];
    

    使用 Fruitcke\Cors

    和 cors 使用默认配置 im

    'paths' => ['api/*'],
    
        /*
        * Matches the request method. `['*']` allows all methods.
        */
        'allowed_methods' => ['*'],
    
        /*
         * Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com`
         */
        'allowed_origins' => ['*'],
    
        /*
         * Patterns that can be used with `preg_match` to match the origin.
         */
        'allowed_origins_patterns' => [],
    
        /*
         * Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers.
         */
        'allowed_headers' => ['*'],
    

    感谢您的支持

    【讨论】:

    • 您的 App Engine 路径端点是否以代码中定义的 api/ 开头?您是否尝试在应用程序代码中添加 Access-Control-Allow-Origin 标头?另请参阅以下GitHub issue。如果这不起作用,请让我知道将\Fruitcake\Cors\HandleCors::class, 移动到$middleware 中的列表顶部是否有任何不同,并确保步骤,如提到的here
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2018-03-29
    • 2021-05-03
    • 2018-08-21
    相关资源
    最近更新 更多