简体   繁体   English

Laravel:用户的 IP 地址在使用负载均衡器和 kubernetes 的后台作业中返回为 127.0.0.1

[英]Laravel : User's IP address is returned as 127.0.0.1 in background jobs using load balancer and kubernetes

I am deploying my laravel application using docker + kubernetes and aws load balancer.我正在使用 docker + kubernetes 和 aws 负载均衡器部署我的 laravel 应用程序。

There is one middleware 'EventLogger' which is run after user logs in to the system.有一个中间件“EventLogger”在用户登录系统后运行。 This logger logs the user's IP address and saves into mongo db.此记录器记录用户的 IP 地址并保存到 mongo db。

Here is that middleware:这是中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use App\Models\EventLogger as Logger;
use App\Models\OauthClient;
use Jenssegers\Agent\Agent;
use App\Models\Advertisement;
use App\Jobs\ProcessEventLogger;

class EventLogger
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $event = null)
    {
        //$request->request->add(["event" => $event]);
        $request->merge(["event" => $event]);
        return $next($request);
    }

    public function terminate($request, $response)
    {
        if($response->getStatusCode() !=  200){
            return;
        }

        $response_contents = $response->getContent();

        //allow only status = true responses
        if(false == json_decode($response_contents)->status){
            return;
        }

        if (env('APP_ENV') === 'testing') {
            $client_id = 3;
        } else{
            if ($request->event == Logger::LOGIN) {
                $client_id = $request->client_id;
            } else {
                $client_id = Auth::user()->token()->client_id;
            }
        }

        $agent = new Agent();
        $agent->browser();
        $agent->device();
        $agent->platform();

        ProcessEventLogger::dispatch($request->all(),$response,  Auth::user()->id, $request->ip(),$client_id, $response_contents, $request->coin_id,$agent->browser(),$agent->device(),$agent->platform());
   }
}

ProcessEventLogger is the background queued job, which is working fine. ProcessEventLogger是后台排队作业,工作正常。

The problem is $request->ip() always returns 127.0.0.1问题是$request->ip()总是返回127.0.0.1

I have tried most of the solutions which are answered here but there is one solution which uses trusted proxies of laravel middleware.我已经尝试了大多数在此处得到解答的解决方案,但有一种解决方案使用 laravel 中间件的受信任代理。

How can I use that feature, as I do not want protected $proxies = '*' and also I can't provide proxy ip address as everytime I restart the pod, new ip address is generated.如何使用该功能,因为我不想要protected $proxies = '*'并且我无法提供代理 ip 地址,因为每次我重新启动 Pod 时,都会生成新的 ip 地址。

Please guide me in this situation.请在这种情况下指导我。

as I do not want protected $proxies = '*'因为我不想要受保护的 $proxies = '*'

Sorry, looks like you don't have many options.抱歉,您似乎没有太多选择。 Either you will have to know the IP Range to allow.要么您必须知道 IP 范围才能允许。 OR Configure your web server accordingly.或相应地配置您的 web 服务器。 Quoting from Symfony Doc which Laravel uses.引用 Laravel 使用的 Symfony 文档。

Some reverse proxies (like AWS Elastic Load Balancing) don't have a static IP address or even a range that you can target with the CIDR notation.一些反向代理(如 AWS Elastic Load Balancing)没有 static IP 地址,甚至没有您可以使用 CIDR 表示法定位的范围。 In this case, you'll need to - very carefully - trust all proxies.在这种情况下,您需要非常小心地信任所有代理。

Configure your web server(s) to not respond to traffic from any clients other than your load balancers.将您的 web 服务器配置为不响应来自负载均衡器以外的任何客户端的流量。 For AWS, this can be done with security groups.对于 AWS,这可以通过安全组来完成。

Once you've guaranteed that traffic will only come from your trusted reverse proxies, configure Symfony to always trust incoming request:一旦您保证流量只会来自您受信任的反向代理,请配置 Symfony 以始终信任传入请求:

https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM