Storing username in nginx logs

Solution 1:

Yes, this is possible. However, since you aren't using HTTP basic auth, you will need your application to tell Nginx what the current username is. Add a response header along the lines of:

X-Username: nishant

Then in your log_format directive, use the variable $sent_http_x_username. This behavior is documented here: http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER

I also recommend removing this extra header from the response before sending it to the client. You can do this with the NginxHttpHeadersMoreModule.

more_clear_headers 'X-Username';