I want to configure nginx so that it parses the http headers and add new parsed strings to access logs. Here is the specific scenario I need:
There is X-Forwarded-For header in incoming requests which contains multiple IP (clients' IP + some proxy server IPs).
X-Forwarded-For = "1.2.3.4, 5.6.7.8"
Here is log_format configuration in my nginx:
log_format main '{"timestamp":"$time_iso8601",'
'"clientIp":"$http_x_forwarded_for",'
'"conSerial":"$connection",'
'"agent":"$http_user_agent"}';
What I want here is parse X-Forwarded-For header and extract proxy server's IP and add it in log format with a sepearate tag like this :
log_format main '{"timestamp":"$time_iso8601",'
'"clientIp":"$http_x_forwarded_for",'
'"proxy": "5.6.7.8",'
'"conSerial":"$connection",'
'"agent":"$http_user_agent"}';
Note that 1.2.3.4 is clients real ip and 5.6.7.8 is proxy server's ip.
Thanks in advance, any help is appreciated.