Nginx – Forward requesting IP from Nginx to Apache’s logs

apache-2.2nginxvirtualhost

I'm using nginx as a front end reverse proxy for Apache, I have the following configuration inside of nginx:

location / {
  if (-f $request_filename) {
      add_header X-Static hit;
      access_log off;
    }

  if (!-f $request_filename) {
      proxy_pass https://127.0.0.1:8000;
      add_header X-Static miss;
    }

With apache listening on port 8000 locally. When I look at apache's logs all the requests come from 127.0.0.1:443 (which is where nginx is sitting). I want to forward the real IP to apache so that it stores it properly in the logs.

I tried adding the following lines to the location block to no avail

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Am I missing a step here? Do I need to change apache's log format? It is currently using the default combined log.

Best Answer

you need to change apache's log format to support x-forward

for example

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{X-Forwarded-For}i" combined

Then you can use

 CustomLog logs/access_log combined

So the last entry in your log will be the header nginx is setting for the real IP. Of course you can switch around the order in the LogFormat line