簡體   English   中英

在Laravel中通過apache反向代理獲取Nuxt應用的真實用戶ip

[英]Get real user ip of Nuxt application through apache reverse proxy in Laravel

我使用 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:

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

但是在這些步驟之后,當我在 Laravel 中使用request()->ip()request()->getClientIp()時,它給了我服務器 ip。 如何在我的 Laravel 應用程序中獲得真實的用戶 ip?

也許您正在尋找的 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM