skip to Main Content

Im thinking on how to retrieve Django user data on the user authetication class and pass it to Nginx session variables, then on the nginx logging settings use that data to create a Nginx access log entry that contains the Django user that create such a request.

I have found these ideas:

  1. Get current request by Django’s or Python threading
    https://gist.github.com/vparitskiy/71bb97b4fd2c3fbd6d6db81546622346
    https://nedbatchelder.com/blog/201008/global_django_requests.html

  2. Set a session variable:
    How can I set and get session variable in django?

  3. And then log the cookie variable via a Nginx configuration like:
    https://serverfault.com/questions/223584/how-to-add-recently-set-cookies-to-nginxs-access-log
    https://serverfault.com/questions/872375/what-is-the-difference-between-http-cookie-and-cookie-name-in-nginx

Any better idea?. I’m reinventing the wheel?

2

Answers


  1. Chosen as BEST ANSWER

    Finally I have done this. Place a middleware en Django that insert in the cookies the logging data that I want nginx to log.

    Then I used the $upstream_cookies_NAME to rescue the COOKIES['NAME'] if any.


  2. have you read django’s documentation on logging?

    I haven’t worked with nginx, yet, but with apache djangos default logger also outputs to the apache log, meaning that you can do this:

    from logging import getLogger
    logger = getLogger('django')
    
    def my_view(request):
        logger.info(f'my view: {request.user}') 
    

    which will output the user to the server log.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search