Search code examples
apacheloggingcentoshttpd.conf

Getting client ip in apache


Here is my logging config:

LogFormat "{\
\"time\":%{sec}t,\
\"clientip\":\"%{CLIENTIP}e\",\
\"status\":%>s,\
\"request_method\":\"%m\",\
\"jsessionid\":\"%{JSESSIONID}C\",\
\"uniqueid\":\"%{X-Unique-Id}i\",\
\"request_processing_time\":%D,\
\"sent_bytes\":%B,\
\"request_uri_query\":\"%q\",\
\"referer\":\"%{Referer}i\",\
\"request_uri_path\":\"%U\",\
\"virtual_host\":\"%{Host}i\",\
\"user_agent\":\"%{User-agent}i\",\
\"request_http_version\":\"%H\",\
\"node\":\"%{HOSTNAME}e\",\
\"x-forwarded-for\": \"%{X-Forwarded-For}i\"\
}" my_logs_json

I need to get only the left most ip address from x-forwarded-for. Is there a way I can get it in apache ? What I have tried is Get correct IP but it hasn't worked for me or I am doing it wrong.

Update

I have been doing it wrong. I needed to pass the variable I had set in the vhost as described in the Get correct IP post, in the LogFormat instead of "%{X-Forwarded-For}i".


Solution

  • I was not passing name of the variable I had set in the vhost and instead I was leaving "%{X-Forwarded-For}i" as it is. Added variable name instead of this and it solved the issue.