Apache environment variables in Django 1.6

198 views
Skip to first unread message

Jon Dufresne

unread,
Nov 20, 2013, 10:16:47 PM11/20/13
to django...@googlegroups.com
Hi,

I recently upgraded my Django application from 1.5 to 1.6. This
application runs on Apache with mod_wsgi. After upgrading, Django
seems unable to access environment variables set by Apache using
SetEnv and SetEnvIf. Using Django 1.5 I was able to access this using
the following recipe in wsgi.py:

---
import os

from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()

def application(environ, start_response):
os.environ['MY_SETTING'] = environ['MY_SETTING']
return _application(environ, start_response)
---

Then, in settings.py, I would access MY_SETTING using os.environ['MY_SETTING'].

Is this a bug that this no longer works in Django 1.6? Is there a
better way to access Apache environment variables?

Thanks

Mike Starov

unread,
Nov 21, 2013, 11:46:52 AM11/21/13
to django...@googlegroups.com
I encountered same issue in my deployment. Have you found a solution?

Jon Dufresne

unread,
Nov 21, 2013, 2:31:15 PM11/21/13
to django...@googlegroups.com
On Thu, Nov 21, 2013 at 8:46 AM, Mike Starov <mikes...@gmail.com> wrote:
> I encountered same issue in my deployment. Have you found a solution?
>

Yes I did. I am still not sure if this is a bug or intentional. It
appears that in 1.6, settings.py is now imported *before* the first
run of the WSGI application. Therefore the settings.py is loaded
before the environment variables can be setup. I now load the WSGI
application as late as possible using the following code. This way,
the settings.py isn't imported until I've received the environ from
Apache. Please feel free to use it to fix your project:

---
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
_application = None

def application(environ, start_response):
os.environ['MY_SETTING'] = environ['MY_SETTING']
global _application
if _application is None:
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
return _application(environ, start_response)
---

Mike Starov

unread,
Nov 22, 2013, 12:16:01 PM11/22/13
to django...@googlegroups.com
Very nice. I have moved import inside the application call but was concerned about evaluating that impot on every request. Looks like you got that covered.

With you setup is it possible to run into some race conditions when two simultaneous request coming for the first time when _application is still uninitialized?

Jon Dufresne

unread,
Nov 22, 2013, 1:24:08 PM11/22/13
to django...@googlegroups.com
> With you setup is it possible to run into some race conditions when two simultaneous request coming for the first time when _application is still uninitialized?

That is a good question, to which I do not have a good answer. If you
come across any issues with this solution I'd be interested to hear
about them.

I filed a ticket #21486 <https://code.djangoproject.com/ticket/21486>.
If you have any useful information to provide, please add it to the
ticket. This concern over race conditions might be a good thing to
mention.

Cheers,
Jon
Reply all
Reply to author
Forward
0 new messages