相关疑难解决方法(0)

如何避免使用Django,nginx和uWSGI将环境变量放入多个位置?

我正在尝试配置nginx + uWSGI来为我的Django应用程序提供服务.

当我将环境变量放入myapp_uwsgi.ini:

uid = username
gid = username
env = DJANGO_SITE_KEY="..."
Run Code Online (Sandbox Code Playgroud)

它按预期工作.

但是,我的应用程序有一些管理命令,它们也应该可以访问我定义的环境变量.

如果我把环境变量放到/home/username/.bashrc:

export DJANGO_SITE_KEY="..."
Run Code Online (Sandbox Code Playgroud)

uWSGI不加载它们.

我试图将环境变量放入一个单独的文件中:

#!/bin/sh
export DJANGO_SITE_KEY="..."
Run Code Online (Sandbox Code Playgroud)

然后从两个调用它.bashrc:

. /home/username/environment
Run Code Online (Sandbox Code Playgroud)

并且myapp_uwsgi.ini:

exec-pre-app = . /home/username/environment
Run Code Online (Sandbox Code Playgroud)

在uWSGI日志中,我看到这一行:

running ". /home/username/environment" (pre app)...
Run Code Online (Sandbox Code Playgroud)

但我的Django应用程序无法访问环境变量os.environ.

我也曾尝试把export命令到preactivatevirtualenvwrapper的钩和使用virtualenv =uWSGI的设置,但它并没有太多的工作(我假设使用virtualenvwrapper命令喜欢当钩子才执行workon.

django nginx uwsgi

13
推荐指数
1
解决办法
6032
查看次数

如何将环境变量传递给 uwsgi?

我在 Apache 下配置了一个 WSGI 处理程序,并且在 Apache 虚拟主机配置中定义了一些环境变量。

SetEnv APP_CONFIG "/var/lib/myapp/app.config"
SetEnv LOG_CONFIG "/var/lib/myapp/app.logging.yml"
Run Code Online (Sandbox Code Playgroud)

为了在开发中测试处理程序而无需安装和配置 Apache,我使用了uWSGI--http选项。

uwsgi --http :9090 --ini uwsgi.ini --wsgi-file wsgi.py
Run Code Online (Sandbox Code Playgroud)

wsgi.py

def application(environ, start_response):
    config_file_path = environ['APP_CONFIG']
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"]
Run Code Online (Sandbox Code Playgroud)

使用 uWSGI http 服务器,如何将这些变量作为environ参数的一部分传递给我的应用程序?

我尝试在 uwsgi.ini 文件中设置环境变量:

[uwsgi]
env = APP_CONFIG="/var/lib/myapp/app.config"
Run Code Online (Sandbox Code Playgroud)

但我得到:

File "wsgi.py", line 5, in application
config_file_path = environ['APP_CONFIG']
KeyError: 'APP_CONFIG'
Run Code Online (Sandbox Code Playgroud)

python wsgi uwsgi

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

uwsgi ×2

django ×1

nginx ×1

python ×1

wsgi ×1