Ordering of custom Django middleware -
in django app, have 2 middlewares.
one called nowwwredirectmiddleware
, it's purpose redirect www
no www
.
the second called xforwardedformiddleware
. populates remote_addr
first value of http_x_forwarded_for
(if exists).
my question middleware ordering in settings.py
. currently, have ordered so:
'myproj.middleware.nowwwredirect.nowwwredirectmiddleware', 'myproj.middleware.xforwardedfor.xforwardedformiddleware', ################## other middleware ################### 'user_sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware',
my question is: shouldn't top 2 other way around? i'm hazy how order them, , going gut feel.
from documentation:
during request phase, before calling view, django applies middleware in order it’s defined in middleware, top-down.
so seems ordering correct - if user redirected no www
, no sense in setting remote address variable. side note - redirecting www
no www
should done web server (apache / nginx), more efficient way.
Comments
Post a Comment