Skip to content

Commit 2692957

Browse files
Fix IP address retrieval when using reverse proxy
1 parent 3e3812c commit 2692957

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

config/user-monitoring.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
*/
8989
'action_monitoring' => [
9090
'table' => 'actions_monitoring',
91-
9291
/*
9392
* Monitor actions.
9493
*
@@ -100,6 +99,14 @@
10099
'on_read' => true,
101100
'on_restore' => false,
102101
'on_replicate' => false,
102+
103+
/**
104+
* Determines if the application should use reverse proxy headers to fetch the real client IP
105+
* If set to true, it will try to get the IP from the specified header (X-Real-IP or X-Forwarded-For)
106+
* This is useful when using reverse proxies like Nginx or Cloudflare.
107+
*/
108+
'use_reverse_proxy_ip' => false,
109+
'real_ip_header' => 'X-Forwarded-For'
103110
],
104111

105112
/*

src/Traits/Actionable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ private static function insertActionMonitoring(mixed $model, string $actionType)
7373
'browser_name' => $detector->getBrowser(),
7474
'platform' => $detector->getDevice(),
7575
'device' => $detector->getDevice(),
76-
'ip' => request()->ip(),
76+
'ip' => config('user-monitoring.use_reverse_proxy_ip')
77+
? request()->header(config('user-monitoring.real_ip_header'))
78+
?: request()->ip()
79+
?: request()->ip()
80+
: request()->ip(),,
7781
'page' => request()->url(),
7882
'created_at' => now(),
7983
'updated_at' => now(),

0 commit comments

Comments
 (0)