我在这里查看文档:{{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;
有什么想法吗?