On an ubuntu machine hosting my Django app (with postgres backend), my env variables were prefectly detected when I fired up gunicorn as my sole webserver using the command gunicorn --bind 0.0.0.0:8080 --env DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application
Next, I installed nginx and configured it to act as a reverse proxy with gunicorn (using a digital ocean guide here). There is no supervisor. This new web server configuration fired up correctly, except now it doesn't detect the env variables at all.
Imagine my env variables are awake=1 and secret=abc123. I've already tried putting export awake=1 and export secret=abc123, in /etc/default/nginx, in gunicorn.conf, in /etc/environment (which sets them globally). I also tried adding them to nginx.conf as env awake=1; and env secret=abc123;.
Nothing worked.
Now it seems that nginx
removes all environment variables inherited from its parent process except the TZ variable
Source: http://nginx.org/en/docs/ngx_core_module.html#env Could this be the reason why nothing I'm trying has come close to working? Nevertheless, echo $awake yields 1 on the command line, which tells me that perhaps the variables are set, but being bypassed or overlooked.
It's become very frustrating. Can anyone help me figure this one out? Thanks in advance.
wsgi.py:
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.wsgi import get_wsgi_application from dj_static import Cling application = Cling(get_wsgi_application()) gunicorn.conf:
description "Gunicorn application server handling myproject" start on runlevel [2345] stop on runlevel [!2345] respawn setuid myuser setgid www-data chdir /home/myuser/directory/myproject/ exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn --chdir=/home/myuser/directory/ --workers 3 --bind unix:/home/myuser/directory/myproject/myproject.sock --env DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application /etc/nginx/sites-available/myproject:
server { listen 80; server_name myapp.cloudapp.net; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/myuser/directory/myproject; } location / { include proxy_params; proxy_pass http://unix:/home/myuser/directory/myproject/myproject.sock; } } Note: Please ask for more information if you need it
os.environ.get('awake')for instance. It's served me well for a month. Just recently, I've been migrating my infrastructure & changed my webserver setup to include nginx as a reverse proxy. Now I've run into this wall where env variables that were previously being detected (and still seem to be correctly set if one doesprintenv) no longer have any effect on settings.py. The application code hasn't changed. Setting them in /etc/environment had no effect either!