如何在Laravel Vapor应用程序中获取HTTP请求的IP? [英] How can I get the IP of an HTTP request in a Laravel Vapor application?

查看:86
本文介绍了如何在Laravel Vapor应用程序中获取HTTP请求的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将Laravel应用程序从服务器移到了Vapor.此应用程序依赖于使用 Request :: ip()记录请求IP地址,但是由于切换到Vapor,所有IP都记录为127.0.0.1.

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.

我在 https://laravel上查看了受信任的代理文档.com/docs/5.6/requests#configuring-trusted-proxies ,但我们没有设置负载均衡器,因此该解决方案似乎不相关.我怀疑此IP地址来自Amazon API Gateway.

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.

我们如何在部署在Vapor上的应用程序中获取传入请求的 actual 客户端IP?

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

下面是我们如何使用IP地址的一个简单示例:

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

    }

推荐答案

我们没有设置负载均衡器",是的.从根本上说,API网关是完全与受信任的代理配置所针对的那种代理.

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

设置'代理'=>您的 config/trustedproxy.php 文件中的"*" ,您应该开始看到正确的IP地址.

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

这篇关于如何在Laravel Vapor应用程序中获取HTTP请求的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆