Correct IP in nginx logs behind reverse proxy / load balancer

Problem:

So I finally got a log solution I like working properly. Logs in the load-balancer are correct in terms of their external IP. The problem is when I look at the webserver logs, this has the IP of the load balancer as the client.

Solution:

We need to add some information to the webserver. Here's the original:

server {
        listen 80;
        
        root /example;
        server_name example.dchidell.com example;
        
        location / {
                try_files $uri $uri/ =404;
        }
                
} 

Here's the new:

server {
        listen 80;
        
        root /example;
        server_name example.dchidell.com example;
        real_ip_header      X-Forwarded-For;
        set_real_ip_from        172.18.0.0/16;
        real_ip_recursive   on;
        proxy_set_header X-Real-IP $remote_addr;
        location / {
                try_files $uri $uri/ =404;
        }
                
} 

We've added the following:

real_ip_header      X-Forwarded-For;
set_real_ip_from        172.18.0.0/16;
real_ip_recursive   on;
proxy_set_header X-Real-IP $remote_addr;

The only part you'll need to change is this:

set_real_ip_from        172.18.0.0/16;

This specifies the IP range of your load balancer / reverse proxy, so the range to trust before the translation occurs. This should be the load balancers / reverse proxies IP as it appears to the nginx webserver. Failing to set this correctly will result in failure.