1

我在 Amazon EC2 中部署 Laravel REST API 并使用 Application Load Balance (ALB) 作为反向代理。因此,架构基本上是 Client -> Route 53 (resolve DNS) -> Load Balancer ALB (reverse proxy) -> EC2 instance with Ubuntu and my Laravel Application

由于 Laravel 使用 Symfony 库来处理代理,我按照这个 Laravel 文档这个 Symfony 文档将我的 ALB 设置为我的 Laravel 应用程序中的受信任代理,使我的 EC2 实例只能通过我的 ALB 访问 - 但我也可以通过以下方式访问我的 EC2 SSH - 给我留下App\Http\Middleware\TrustProxies如下:

<?php

namespace App\Http\Middleware;

use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string|null
     */
    protected $proxies = '*';

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB; // Request::HEADER_X_FORWARDED_ALL;
}

因此,显然我的应用程序可以通过反向代理标头从客户端接收 HTTP 标头,特别是HEADER_X_FORWARDED_AWS_ELB.

但是,客户端应用程序根本没有接收 HTTP 响应标头,例如 CORS(我正在使用 Laravel 默认\Fruitcake\Cors\HandleCors库来处理 CORS)或'Accept': 'application/json',导致客户端出现 CORS 错误。

因此,换句话说,我的 Laravel 应用程序响应标头不会通过我的反向代理 ALB。

有谁知道发生了什么以及我如何/是否可以解决这个问题?

4

1 回答 1

1

It might be useful to verify that the HTTP response exiting the EC2 instance contains the correct reply headers. You could run tcpdump on the EC2 instance to verify that. That will at least confirm if it is the ALB that is causing the problem.

于 2021-03-09T10:30:46.420 回答