【问题标题】:Get real user ip of Nuxt application through apache reverse proxy in Laravel通过 Laravel 中的 apache 反向代理获取 Nuxt 应用程序的真实用户 ip
【发布时间】:2021-03-04 13:19:22
【问题描述】:

我使用 Nuxtjs 作为我的应用程序的前端和 Laravel 框架作为后端。 我将我的应用程序托管在 Apache Centos 服务器上。 在 localhost:3000 上使用 pm2 运行应用程序后,我无法在 Laravel 中获取用户的真实 IP。

我的 Centos 版本:

Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1127.10.1.el7.x86_64
Architecture: x86-64

我的 Apache 版本:

Server version: Apache/2.4.43 (Unix)

我在 apache 中的虚拟主机:

<VirtualHost MY_IP:80>
    UserDir public_html
    DocumentRoot /var/www/html
    ServerName example.com
    ProxyPreserveHost On
    RemoteIPHeader X-Forwarded-For
    RemoteIPProxiesHeader X-Forwarded-By
    <LocationMatch "/">
        allow from all
        Satisfy any
        AllowOverride All
        Require all granted
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </LocationMatch>
</VirtualHost>

我在 Laravel 中 AppServiceProvider 中的 boot 方法

public function boot()
{
    Request::setTrustedProxies(['REMOTE_ADDR'], Request::HEADER_X_FORWARDED_FOR);
}

但是当我使用 request()-&gt;ip()request()-&gt;getClientIp() 时,经过这些步骤 在 Laravel 中,它给了我服务器 ip。 如何在我的 Laravel 应用程序中获取真实用户 ip?

【问题讨论】:

    标签: laravel apache vue.js nuxt.js reverse-proxy


    【解决方案1】:

    也许是您正在寻找的 HTTP_X_FORWARDED_FOR http 标头变量。

    $_SERVER['HTTP_X_FORWARDED_FOR']
    

     $_SERVER["HTTP_X_REAL_IP"]
    

    如果它不存在 Apache 的 mod_remoteip 模块可以帮助这个头变量 更多关于:https://www.globo.tech/learning-center/x-forwarded-for-ip-apache-web-server/

    【讨论】:

    • 感谢@Mustafa 的回复。但 $_SERVER 变量中没有 HTTP_X_FORWARDED_FOR 索引
    • 我在我的应用程序中使用与您相同的设置 nuxt + laravel 唯一的区别是 nginx,所以我是 nginx 人,但这有帮助吗? globo.tech/learning-center/x-forwarded-for-ip-apache-web-server
    • 在我的 apache 网络服务器中:a2enmod: command not found
    • 如果没有相关主题,请尝试使用 sudo stackoverflow.com/questions/50022485/…
    • 我使用 sudo 但它说command not found。我列出了我的 apache 模块,我看到了这个 remoteip_module (static)
    猜你喜欢
    • 2020-10-21
    • 2020-05-31
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多