B4J Question [Server] [solved] How to change the IP address in b4j-2017_MM_dd.request.log

billzhan

Active Member
Licensed User
Longtime User
Hi,

If the server is running behind a reverse proxy(e.g., Nginx), the ip address in the b4j-xx.log will be the IP for the proxy.

The real ip can be got in the handler by
B4X:
realIP =  Request.GetHeader("X-Real-IP")

Is it possible to override the one in b4j-xx.log with the real one or add the real IP to the log?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to log the ip address based on the X-Forwarded-For header:
B4X:
srvr.Start
   
   For Each h As JavaObject In GetHandlers
     If GetType(h) = "org.eclipse.jetty.server.handler.RequestLogHandler" Then
       Dim requestLog As JavaObject = h.RunMethod("getRequestLog", Null)
       requestLog.RunMethod("setPreferProxiedForAddress", Array(True))
     End If
   Next
End Sub

Sub GetHandlers As Object()
   Dim josrvr As JavaObject = srvr
   josrvr = josrvr.GetField("server")
   Dim jo As JavaObject = josrvr
   Do While GetType(jo) <> "org.eclipse.jetty.server.handler.HandlerCollection"
     jo = jo.RunMethodJO("getHandler", Null)
   Loop
   Return jo.RunMethod("getHandlers", Null)
End Sub
 
Upvote 0
Top