Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way in which user-name or user-id can be stored in nginx logs. I tried inserting $remote_user in the log_format directive but it doesn't seem to work.

share|improve this question
up vote 6 down vote accepted

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:

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';
share|improve this answer
Can you please tell me how to add a response header? – nishant Jun 10 '14 at 18:45
@nishant That depends entirely on your code. You will have to write something or modify what you have to output the username in the response header. – Brad Jun 10 '14 at 18:46
The web application is based on django. – nishant Jun 10 '14 at 18:48
@nishant I haven't written anything with Django so I can't give you specific advice, but it looks like it supports middleware. I would write a middleware module that takes care of adding the extra header. – Brad Jun 10 '14 at 18:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.