Possible to log X-FORWARDED-FOR to nginx error_log?

skrewler asked:

Nginx allows custom log formats for access logs — right now I’m logging http_x_forwarded_for and it’s working fine.

The problem is my error log only shows the LB IP. After Googling and reading through their documentation, it seems error_log doesn’t support a custom format, only setting the log level (info, alert, notice, warning, etc).

Is there any kind of workaround or module I could use to get around this?

For completeness, my setup is:

Amazon ELB -> pool of instances running nginx as a reverse proxy to php-fpm 7.0

My answer:

You should be using the real IP module, so that the client’s actual IP address is considered the remote IP address, rather than your load balancer’s IP address. This way, you won’t have to check X-Forwarded-For in your logs, nor in your application either.

It is simple to enable it, just supply your load balancer IP address(es):

real_ip_header    X-Forwarded-For;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.