简体   繁体   中英

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

I am using Nuxtjs as frontend of my application and Laravel framework as backend. I hosted my application on an Apache Centos server. After running application with pm2 on localhost:3000 I can not get real ip of user in Laravel.

My Centos version:

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

My Apache version:

Server version: Apache/2.4.43 (Unix)

My VirtualHost in 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>

My boot method in AppServiceProvider in Laravel

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

But after these steps when I use request()->ip() or request()->getClientIp() in Laravel, it gives me server ip. How can i get real user ip in my Laravel application?

Maybe HTTP_X_FORWARDED_FOR http header variable that you looking for.

$_SERVER['HTTP_X_FORWARDED_FOR']

or

 $_SERVER["HTTP_X_REAL_IP"]

if its doesn't exists Apache's mod_remoteip module can help this header variables more about: https://www.globo.tech/learning-center/x-forwarded-for-ip-apache-web-server/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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