TrustProxies和Laravel返回服务器的IP地址,而不是最终用户的IP地址

时间:2018-10-11 15:30:32

标签: php laravel proxy laravel-5.6

我在这里查看文档:{​​{3}}和fideloper/TrustedProxy

我与Laravel一起建立,由Forge管理服务器。

这是Kernel.php中定义的中间件:

protected $middleware = [
    \App\Http\Middleware\EnvMiddleware::class,
    \App\Http\Middleware\TrustProxies::class,
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \ChaseConey\LaravelDatadogHelper\Middleware\LaravelDatadogMiddleware::class
];

这是TrustProxies.php

<?php

namespace App\Http\Middleware;

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

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

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

在API调用中,$request->ip()分阶段返回正确的IP地址-负载不平衡。

但是,这将返回生产中我的两个负载平衡服务器之一的值。

这些是已安装的composer.json软件包:

"laravel/framework": "5.6.*"

"fideloper/proxy": "^4.0"

我都尝试过:

protected $headers = Request::HEADER_X_FORWARDED_ALL;

protected $headers = Request:: HEADER_X_FORWARDED_AWS_ELB;

有什么想法吗?

0 个答案:

没有答案