如何在nginx.conf中引用OS环境变量 [英] How to reference OS Environment Variables in nginx.conf

查看:482
本文介绍了如何在nginx.conf中引用OS环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在nginx.conf中.

In nginx.conf.

通过set $name value设置变量后, 我可以像$name一样引用它,

After set a variable by set $name value, i can reference it like $name,

但是当我导出OS环境变量时 通过env name_from_env, 像 https://nginx.org/en/docs/ngx_core_module.html#env说, 而且我确定name_from_env是有效的 从nginx的父进程定义.

But when I export an OS Environment Variable by env name_from_env, like https://nginx.org/en/docs/ngx_core_module.html#env said, and i am sure the name_from_env is valid which defined form nginx's parent process.

但是,我的朋友们,如何引用它? $ name_from_env或$ {name_from_env}或 %name_from_env%无法正常工作.

But, my friends, how to reference it ? $name_from_env or ${name_from_env} or %name_from_env% didn't work what I've tried before.

推荐答案

nginx当前不具有在配置中引用其环境变量的内置功能.但是,最简单的解决方案是来自 ngx_http_perl_module perl_set指令, nginx.官方的nginx包装动态地构建Perl模块,因此这是确保您安装额外的附件的一种情况. nginx-module-perl软件包(或配置自定义的Nginx构建,如果您正在这样做的话).

nginx doesn't have the built-in ability to reference its environment variables in the configuration at present. The simplest solution however is the perl_set directive from ngx_http_perl_module, an extra module for nginx. The official nginx packaging builds the Perl module dynamically so it's a case of ensuring you install the extra nginx-module-perl package (or configure your custom build of nginx, if that's what you're doing).

配置如下:

# Make environment variable available
env NAME_FROM_ENV;
# Load dynamic module (if built with Perl as dynamic module; omit if static)
load_module modules/ngx_http_perl_module.so;

http {
    server {
        location / {
            # Use Lua to get get and set the variable
            perl_set $name_from_env 'sub { return $ENV{"NAME_FROM_ENV"}; }';
            ...
        }
    }
}

另请参见 https: //docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html ,了解如何使用Lua实现同一件事. Lua支持需要第三方模块,并且不随Nginx的默认软件包一起提供.

See also https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html for how to use Lua to achieve the same thing. Lua support requires a third party module and isn't shipped with nginx's default packages.

这篇关于如何在nginx.conf中引用OS环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆