我有困難想同時獲得Larvel TrustedProxy兩者的CloudFlare和AWS的EC2彈性負載均衡共同努力。它假定您只使用1個代理,而不是2個,並且不能正常工作。我最終只是將可信代理的東西全部跳過,並製作一個簡單的MiddleWare,如果使用CloudFlare,無論您在前面有其他Load Balancer,它都會設置正確的IP地址和協議。
的CloudFlare通過HTTP頭將有關客戶的信息:https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-CloudFlare-handle-HTTP-Request-headers-
我們正在對客戶端IP和客戶端HTTP/HTTPS協議和設置,在$請求 - >服務器屬性。
======
應用程序/ HTTP /中間件/ UseCloudFlareHeaders.php
<?php namespace App\Http\Middleware;
use Closure;
class UseCloudFlareHeaders
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//if CloudFlare request, set correct protocol and proper client ip address
$HTTP_CF_VISITOR = $request->server->get('HTTP_CF_VISITOR');
$HTTP_CF_CONNECTING_IP = $request->server->get('HTTP_CF_CONNECTING_IP');
if ($HTTP_CF_VISITOR and json_decode($HTTP_CF_VISITOR)->scheme == 'https') {
$request->server->set('HTTPS', 'on');
}
if ($HTTP_CF_CONNECTING_IP) {
$request->server->set('REMOTE_ADDR', $HTTP_CF_CONNECTING_IP);
}
return $next($request);
}
}
應用程序/ HTTP/Kernal.php
protected $middleware = [
//add this to your middleware...
\App\Http\Middleware\UseCloudFlareHeaders::class
];
如果有人知道更好的做法,請告訴我們。
不需要在apache conf文件或htaccess中設置。以某種方式使mod_geoip。c使用變量HTTP_CF_CONNECTING_IP而不是REMOTE_ADDR(REMOTE_ADDR此刻讀取DNS代理服務器,HTTP_CF_CONNECTING_IP是解決方法) – hellodolly