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

让人想犯罪 __ 提交于 2020-12-29 07:51:47

问题


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);

    }

回答1:


"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.




回答2:


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')



回答3:


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()



来源:https://stackoverflow.com/questions/58346824/how-can-i-get-the-ip-of-an-http-request-in-a-laravel-vapor-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!