2

So I want to invoke different instances of Python code based on user's URL (or at least user's domain) from WSGI file. We're running on Apache 2.x + Python + Django stack.

Looks like there should be something like 'SERVER_NAME' or 'HTTP_HOST' key in the os.environ dictionary accessible in WSGI script (here and here). However this key is missing and here's how os.environ.__dict__ looks like when everything is working properly:

{'data': {'LANG': 'C', 'APACHE_LOCK_DIR': 'xxx', 'TZ': 'US/Pacific', 'DJANGO_SETTINGS_MODULE': 'xxx', 'APACHE_RUN_USER': 'xxx', 'PWD': 'xxx', 'APACHE_PID_FILE': 'xxx', 'CELERY_LOADER': 'django', 'APACHE_RUN_DIR': 'xxx', 'APACHE_LOG_DIR': 'xxx', 'APACHE_RUN_GROUP': 'xxx', 'HOME': 'xxx', 'PATH': 'xxx'}}

Domain/URL is a very basic info and I'm surprised I'm failing to find it.

UPDATE: I've settled on a different approach (using Apache Config instead WSGI script). That seems to solve the issue of missing WSGI environment variable. Thanks for all your responses!

4
  • Neither of your links show using os.environ. Commented Aug 13, 2015 at 5:45
  • Good point. When I tried to access environ in WSGI script though, Apache throws an error: "NameError: name 'environ' is not defined" Commented Aug 13, 2015 at 15:35
  • Did you try doing so inside the WSGI handler? Commented Aug 13, 2015 at 15:36
  • Actually I see what you mean. We might wanna do this filtering at the Apache config level indeed. If you can tell me how to accept a comment as a correct answer I'd do just that. Thanks! Commented Aug 13, 2015 at 16:07

1 Answer 1

3

You're confusing the os.environ dict that shows the current system environment variables with the environ that is passed to the wsgi application as a parameter. The path is in the latter.

However, since you're using Django, that information is all made available via the request object.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Daniel. When I tried to access environ directly in WSGI script, Apache writes this error to logs: "NameError: name 'environ' is not defined". Should we be able to access it from WSGI with correct server configuration? [Note that our objective is to establish subdomain before invoking Django]
Maybe your should show where you are trying to do this? environ is not a global, it is an explicit parameter passed to the wsgi application object for each request. For Django, that object is instantiated inside the wsgi.py, which gets it from django.core.wsgi.get_wsgi_application. What exactly are you doing?
That explains it, Django's request object does have all these fields populated. We wanna route users by loading different Python code for each subdomain. This polymorphism could be done at the VM level (using multiple VMs, one per subdomain), at the code invocation level (what we're trying to do - one server, multiple Python processes) and at the code level (something we wanna avoid by keeping Python/Django code unaware of subdomains). I'm starting to inch towards this as hinted by Ignacio.
Where exactly in the Django request object are you looking? The original WSGI environ dictionary values are located in request.META.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.