配置Nginx和Gunicorn后,环境变量(Django app在Ubuntu机器上)

配置Nginx和Gunicorn后,环境变量(Django app在Ubuntu机器上)

问题描述:

我刚刚为Django网络应用程序使用Nginx(反向代理)设置了Gunicorn。组合似乎按照 gunicorn.log 正确启动。请注意,我没有使用主管。

I just set up Gunicorn with Nginx (reverse proxy) for a Django web app. The combo seems to be firing up correctly as per gunicorn.log. Note that I'm not using supervisor.

但好奇的是,我的环境变量(在 .profile )根本没有收拾! printenv 显示它们存在。我尝试过的一些事情是将环境变量放在 / etc / default / nginx 中,并重新启动nginx,在 etc / environment ,在 .profile 中,在 nginx.conf 中,在 gunicorn.conf 等,它只是不起作用

But curiously, my environment variables (set in .profile) aren't being picked up at all! printenv shows they exist. Some things I've tried are putting the environment variables in /etc/default/nginx and restarting nginx, in etc/environment, in .profile, in nginx.conf, in gunicorn.conf, etc. It just doesn't work!

顺便说一下,在安装和配置nginx之前,即,当我简单运行时: gunicorn --bind 0.0.0.0:8080 --env DJANGO_SETTINGS_MODULE = myproject.settings myproject.wsgi:application

By the way, it worked perfectly before installing and configuring nginx, i.e. when I was simply running: gunicorn --bind 0.0.0.0:8080 --env DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application.

现在看来,nginx:

Now it seems that nginx:


删除从其父进程继承的所有环境变量
除外TZ变量

removes all environment variables inherited from its parent process except the TZ variable

资料来源: http://nginx.org/en/docs/ngx_core_module.html#env 这可能是为什么我没有尝试接近工作的原因?但是如果是这样,添加到nginx.conf中的这些变量应该是我想的。然而,使用 echo $ envvar 在命令行中产生正确的值,这告诉我可能已设置变量,但被绕过或忽略 。请注意, USER env变量也显示为,而打印 TERM 打印 linux 。

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? But if so, these variables added to nginx.conf ought to have been picked up I suppose. Nevertheless, using echo $envvar yields the correct value on the command line, which tells me that perhaps the variables are set, but being bypassed or overlooked. Note that the USER env variable shows up as None too, whereas print TERM prints linux.

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: strong>

/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;
    }
}

注意:要求我提供更多信息

所以一个气密(有希望的)设置环保变量,编辑gunicorn.conf中没有任何麻烦,如下所示:

So an air-tight (hopefully) way of setting environment variables that gunicorn would have no trouble seeing is editing gunicorn.conf as follows:

exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn --chdir = / home / myuser / directory / --workers 3 --bind unix:/home/myuser/directory/myproject/myproject.sock -e var1 = value1 -e var2 = value2 myproject.wsgi:application

这对我来说已经很好了。如果我遇到任何问题,我会更新这个答案。

This has served me well so far. If I run into any issues, I'll update this answer.