简体   繁体   English

AWS Application Load Balancer真实用户ip问题

[英]AWS Application Load Balancer real user ip problem

I run laravel application on AWS Elasticbeanstalk, I use Application Load Balancer.我在 AWS Elasticbeanstalk 上运行 laravel 应用程序,我使用 Application Load Balancer。

Route::get('/what-is-my-ip', function(){ 
    return request()->ip();
});

When I run this code, my ip doesn't show, it shows the load balancer's ip addresses.当我运行此代码时,我的 ip 不显示,它显示负载均衡器的 ip 地址。

Those who used the same problem with cloudflare also experienced and have solutions for cloudflare, but I couldn't find a solution for the AWS Application Load Balancer.那些在 cloudflare 上使用过同样问题的人也经历过并有 cloudflare 的解决方案,但我找不到 AWS Application Load Balancer 的解决方案。

I am having trouble getting users' ip addresses and adding --allow-ip in maintenance mode.我无法获取用户的 ip 地址并在维护模式下添加 --allow-ip。

function real_IP() {

    $real_IP = '';

    if (getenv('HTTP_CLIENT_IP'))
        $real_IP = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $real_IP = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $real_IP = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $real_IP = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $real_IP = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $real_IP = getenv('REMOTE_ADDR');
    else
        $real_IP = 'UNKNOWN';

    return $real_IP;
}

when i run this code it gives the correct ip address, i want to fix it across laravel.当我运行此代码时,它给出了正确的 ip 地址,我想在 laravel 上修复它。

You'll need to trust the AWS load balancers as a proxy .您需要信任 AWS 负载均衡器作为代理

If you are using AWS Elastic Load Balancing, your $headers value should be Request::HEADER_X_FORWARDED_AWS_ELB .如果您使用的是 AWS Elastic Load Balancing,则您的$headers值应为Request::HEADER_X_FORWARDED_AWS_ELB For more information on the constants that may be used in the $headers property, check out Symfony's documentation on trusting proxies.有关可能在 $headers 属性中使用的常量的更多信息,请查看 Symfony 关于信任代理的文档。

If you are using Amazon AWS or another "cloud" load balancer provider, you may not know the IP addresses of your actual balancers.如果您使用的是 Amazon AWS 或其他“云”负载均衡器提供商,您可能不知道实际均衡器的 IP 地址。 In this case, you may use * to trust all proxies:在这种情况下,您可以使用 * 来信任所有代理:

 protected $proxies = '*';

Two common issues when you use AWS or any other cloud Load Balancer:使用 AWS 或任何其他云负载均衡器时的两个常见问题:

  1. HTTPS (Laravel asset and route): You applied SSL/TLS and the URL is protected in the browser but Laravel doesn't load your asset and throw an error. HTTPS (Laravel 资产和路由):您应用了 SSL/TLS,并且 URL 在浏览器中受到保护,但 Laravel 不会加载您的资产并抛出错误。 The error look like it blocks the URLS because of you are trying to load http URL http request.由于您试图加载 http URL http 请求,该错误看起来像阻止了 URL。 Most of the people facing this issue when use AWS or any other cloud Load Balancer .大多数人在使用AWS or any other cloud Load Balancer时都会遇到这个问题。 When running your applications behind a load balancer that terminates TLS / SSL certificates, you may notice your application sometimes does not generate HTTPS links when using the url helper.在终止 TLS / SSL 证书的负载均衡器后面运行应用程序时,您可能会注意到,在使用 Z572D4E421E5E6B9BC11D815E8A027112 帮助程序时,您的应用程序有时不会生成 HTTPS 链接。 Typically this is because your application is being forwarded traffic from your load balancer on port 80 and does not know it should generate secure links.通常这是因为您的应用程序正在从端口 80 上的负载均衡器转发流量,并且不知道它应该生成安全链接。

  2. IP : Another issue is IP issue. IP :另一个问题是 IP 问题。 You can't get the user/visitor IP and it returns always server IP.您无法获取用户/访问者 IP 并且它始终返回服务器 IP。 This issue also happen because of proxies .这个问题也因为proxies而发生。

Solution : When you are using AWS or any cloud Load Balancer then you may not know the exact IP address of your actual Loads Balancer so should allow all proxies like below example. Solution :当您使用AWS or any cloud Load Balancer时,您可能不知道实际Loads Balancer的确切 IP 地址,因此应该允许所有代理,如下例所示。

Use * to allow trust all proxies in your TrustProxies middleware.使用*允许信任TrustProxies中间件中的所有代理。 Here is your middleware app/Http/Middlewares/TrustProxies.php .这是您的中间件app/Http/Middlewares/TrustProxies.php

namespace App\Http\Middleware;

use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;

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

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO;

If you are using AWS Elastic Load Balancing, your $headers value should be Request::HEADER_X_FORWARDED_AWS_ELB.如果您使用的是 AWS Elastic Load Balancing,则您的 $headers 值应为Request::HEADER_X_FORWARDED_AWS_ELB. For more information on the constants that may be used in the $headers property, check out Symfony's documentation on trusting proxies .有关可以在 $headers 属性中使用的常量的更多信息,请查看 Symfony 关于信任代理的文档。

namespace App\Http\Middleware;

use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;

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

I think it solves your HTTPS, IP and other proxy related issue.我认为它解决了您的 HTTPS、IP 和其他代理相关问题。 To read more details read Laravel doc .要阅读更多详细信息,请阅读Laravel 文档 If you face any other issue or need improvements comments below.如果您遇到任何其他问题或需要以下改进意见。

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

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