HOWTO: log the user's IP, not the proxy's, in nginx access log

nginx

nginx

So back in January I had a post about HOWTO: log the user’s IP, not the proxy’s, in Lighttpd access log, but today I switched that system to run nginx (actually nginx has been running since early this year, I just got lazy on running Varnish) fronted again by Varnish. I had the same issue, but not much trouble solving it. Since I often refer to my own notes on fak3r, I’m recording it here for myself, and anyone streaming in from Google. So, as I talked about before, when you run a webserver behind Varnish doing http acceleration, the webserver access logs will display the IP of the proxy (generally 127.0.0.1) instead of the end user’s IP. This not only breaks any kind of tracking or reporting you want to run against your webserver logs. Since this server runs Varnish in front of nginx, and it reveals the end user’s IP in the header as X-Forwarded-For, so it’s just a matter of making nginx use that variable in its access logs instead of the default variable defining the referring IP. Once we know that, the configuration is simple.  Edit your nginx.conf file:

vi /etc/nginx/nginx.conf

Once in the file, find the block about logging, and add the following to it:

  log_format main '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"' ;
  access_log /var/log/nginx/access.log main;

and finally, restart nginx

/etc/init.d/nginx restart

If you look at the logfiles now you’ll see the IP of the original requester!




  • Anonymous

    How are you handling the comprehensive logging? If some clients are served out of varnish cash, won’t their hit be missing from nginx?

    • http://fak3r.com/ fak3r

      Yes, but I’m using Google Analytics on my sites, so I don’t count hits via the logs. Howver, you can run varnishncsa, which outputs logfiles in Apache style syntax, so I suspect that’s the work around people use to do that when using the logs; they just have to run analysis across a range of logs instead of just Apache.

  • Anonymous

    How are you handling the comprehensive logging? If some clients are served out of varnish cash, won’t their hit be missing from nginx?

    • http://fak3r.com fak3r

      Yes, but I’m using Google Analytics on my sites, so I don’t count hits via the logs. Howver, you can run varnishncsa, which outputs logfiles in Apache style syntax, so I suspect that’s the work around people use to do that when using the logs; they just have to run analysis across a range of logs instead of just Apache.

  • WilliamsRuppJimmi990

    Hello Guyz I added this post to my article site. You can view this here.respectpampe nel______________________________________________how to pass a hair drug test | tips on how to pass a drug test | pass drug test

  • Pingback: HOWTO: log the user’s IP, not the proxy’s, in nginx access log | qBlog

  • Pingback: Log user’s IP, not the proxy’s in nginx access log | Nginx Lighttpd Tutorial

Read previous post:
Daily Digest for 2008-12-16

[...]

Close