I
I
Ivan2022-02-02 17:45:37
Django
Ivan, 2022-02-02 17:45:37

How to properly pass HTTP headers to Django?

The challenge was to deploy an application to the Django server that uses the REMOTE_USER header. Nginx inserts an authorized user there and passes it on to the Gunicorn WSGI server. And the latter already transfers to the application. Looking through the headers, I realized that some intermediate instance adds the "HTTP_" prefix. Therefore, I get "HTTP_REMOTE_USER". The question is who adds this prefix and how to remove it?
I set the variable with a directive in NGINX: Gunicorn is configured according to the standard:
proxy_set_header REMOTE_USER $remote_user;

ExecStart=/var/www/portal/venv/bin/gunicorn \
          --access-logfile /var/log/gunicorn/access.log \
          --error-logfile /var/log/gunicorn/error.log \
          --capture-output \
          --log-level debug \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          portal.wsgi:application

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-02-02
@JamesWest

Looking through the headers, I realized that some intermediate instance adds the "HTTP_" prefix.
Instead of building your own guesses, you should have read the doc https://docs.djangoproject.com/en/4.0/ref/request-... well, below about HttpRequest.headers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question