0

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?

Ahmad_kh
  • 151
  • 1
  • 6

1 Answers1

0

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/

Mustafa Acar
  • 412
  • 3
  • 10
  • Thanks @Mustafa for your reply. but there is no `HTTP_X_FORWARDED_FOR` index in $_SERVER variable – Ahmad_kh Nov 21 '20 at 08:51
  • i am using same setup like you nuxt + laravel in my application only difference is nginx so i am nginx guy but is this helps ? https://www.globo.tech/learning-center/x-forwarded-for-ip-apache-web-server/ – Mustafa Acar Nov 21 '20 at 08:53
  • in my apache web server: `a2enmod: command not found` – Ahmad_kh Nov 21 '20 at 09:22
  • try with sudo if its not there is a some related topics https://stackoverflow.com/questions/50022485/a2enmod-command-not-found-in-apache-server-using-cpanel-in-linux-vps – Mustafa Acar Nov 21 '20 at 10:07
  • i used with sudo but it said `command not found`. i listed my apache modules and i saw this `remoteip_module (static)` – Ahmad_kh Nov 21 '20 at 10:19