1

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

  • Where do you need these environment variables used? – Academiphile Dec 10 '15 at 6:51
  • I set postgres and Amazon S3 config in my project's settings.py based on these env variables. I detect them in settings.py via 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 does printenv) no longer have any effect on settings.py. The application code hasn't changed. Setting them in /etc/environment had no effect either! – Hassan Baig Dec 10 '15 at 6:59
  • Is setting them literally in the code not an option? – Academiphile Dec 10 '15 at 7:04
  • Yea, if all else fails, I'll have to make that choice. Ideally I don't want to set my secret keys and what not in the code. Likewise, my application is alive separately on Heroku and Azure both. While on Azure, I want it to detect it's on Azure from the env (ditto for Heroku). – Hassan Baig Dec 10 '15 at 7:07
  • You could read secret key from a text file. But you're right, it's a workaround not a solution. – Academiphile Dec 10 '15 at 7:08
1

I suggest setting your env variables right where you're running gunicorn, like so: gunicorn --bind0.0.0.0:8080 -e var1=value1 -e var2=value2 myproject.wsgi:application. That should work for you.

  • There has got to be a better way, right? Can't gunicorn see os.environ variables without this? – Scott Skiles Oct 27 '18 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.