How can I get the IP of an HTTP request in a Laravel Vapor application?

Viewed 2874

I recently moved a Laravel app from a server to Vapor. This app relies on logging request IP addresses using Request::ip(), but since switching to Vapor, all IPs are logged as 127.0.0.1.

I have looked over the Trusted Proxy docs at https://laravel.com/docs/5.6/requests#configuring-trusted-proxies but we do not have a load balancer set up, so this solution does not appear relevant. I suspect this IP address is coming from the Amazon API Gateway.

How do we get the actual client IP of incoming requests in an app deployed on Vapor?

A minimal example of how we use the IP address is below:

public function store(Request $request)
    {
        $workerIP = $request->ip();
        $worker = Worker::create(['ip_address' => $workerIP]);
        return view('workers.show')->withWorker($worker);

    }
5 Answers

There is x-vapor-source-ip header in latest Vapor core package (vapor-core:v2.2.1) which exposes Lambda's sourceIp property in order to get the client's real ip safely.

You can retrieve the ip:

Request::header('x-vapor-source-ip')

"we do not have a load balancer set up" Yes, you do. The API Gateway is, fundamentally, a proxy of exactly the sort that the trusted proxy configuration is intended for.

Set 'proxies' => '*' in your config/trustedproxy.php file and you should start seeing the right IP addresses.

I faced the same problem. The suggested x-vapor-source-ip does not work when CloudFlare is being used like when you use Vapors temporary URL's. When Laravel Vapor sits behind CloudFlare, the User's IP address can be obtained via:

$request->header('cf-connecting-ip');

On actual domain names (non Vapor domains) use:

$request->header('x-vapor-source-ip');

I talked to Vapor Support today. They told me to keep TrustProxy default value and let Vapor does the magic. It works. This is my TrustProxy

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

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

and to get IP, I m using request()->ip()

If you are looking for the client IP which application is accessing.

then you will check the $_SERVER you will get more information there.

 $_SERVER['HTTP_X_VAPOR_SOURCE_IP']

try out above variable there you get your client ip. also below helper function in Laravel help you to get IP.

function get_client_ip()
{
  $ipaddress = '';
  if (isset($_SERVER['HTTP_CLIENT_IP']))
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  else if (isset($_SERVER['HTTP_X_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  else if (isset($_SERVER['HTTP_X_VAPOR_SOURCE_IP']))
    $ipaddress = $_SERVER['HTTP_X_VAPOR_SOURCE_IP'];
  else if (isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
  else if (isset($_SERVER['REMOTE_ADDR']))
    $ipaddress = $_SERVER['REMOTE_ADDR'];
  else
    $ipaddress = 'UNKNOWN';
  return $ipaddress;
}

You can tried this function. Thanks.

Related