# Conditional access log formatting?

**URL:** <https://www.ruby-forum.com/t/conditional-access-log-formatting/242757>\
**Category:** NGINX\
**Created:** [July 18, 2015, 12:38pm UTC](https://www.ruby-forum.com/t/conditional-access-log-formatting/242757 "2015-07-18T12:38:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 18, 2015, 12:38pm UTC](https://www.ruby-forum.com/t/conditional-access-log-formatting/242757/1 "2015-07-18T12:38:26Z")

</div>

Hi,

I’m using haproxy infront of some NGINX instances, but only for some  
instances which are being loadbalanced by haproxy, there are other  
instances which go straight through.

I realise I need to change the logging format in NGINX to enable  
X-Forwarded-For in the access logs.

The question is whether I can conditionally format the logs, i.e. use  
“X-Forwarded-For” if it exists, otherwise use the normal NGINX logging  
format.

I’ve had a quick peek at the manual, I’m thinking maybe this can be done  
with a map of some sort ? Maybe there’s a better way ? To be honest  
I’ve no idea where to start, and I’ve never used maps before anyway !

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [July 18, 2015, 8:03pm UTC](https://www.ruby-forum.com/t/conditional-access-log-formatting/242757/2 "2015-07-18T20:03:34Z")

</div>

## I suggest you use the ‘if’ parameter of 2 access\_log directive (\>v1.7.0), combined with 2 maps with reversed logic.

_B. R._

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [July 20, 2015, 2:49pm UTC](https://www.ruby-forum.com/t/conditional-access-log-formatting/242757/3 "2015-07-20T14:49:07Z")

</div>

On Saturday 18 July 2015 11:37:33 Ben wrote:

> “X-Forwarded-For” if it exists, otherwise use the normal NGINX logging  
> format.
> 
> I’ve had a quick peek at the manual, I’m thinking maybe this can be done  
> with a map of some sort ? Maybe there’s a better way ? To be honest  
> I’ve no idea where to start, and I’ve never used maps before anyway !

You should use the realip module:

> **[Module ngx\_http\_realip\_module](http://nginx.org/en/docs/http/ngx_http_realip_module.html)**

wbr, Valentin V. Bartenev
