在 nginx.conf 中。
设置变量后set $name value
,
我可以像这样参考它$name
,
但是当我导出操作系统环境变量时
经过env name_from_env
,
like https://nginx.org/en/docs/ngx_core_module.html#env https://nginx.org/en/docs/ngx_core_module.html#env说,
我确信 name_from_env 是有效的
从 nginx 的父进程定义。
但是,朋友们,如何参考呢?
$name_from_env 或 ${name_from_env} 或
%name_from_env% 不起作用,我之前尝试过。
目前,nginx 没有内置的在配置中引用其环境变量的功能。然而,最简单的解决方案是perl_set
指令来自ngx_http_perl_模块 http://nginx.org/en/docs/http/ngx_http_perl_module.html,nginx 的额外模块。 nginx官方包装动态构建 Perl 模块 http://nginx.org/en/linux_packages.html#dynmodules所以这是确保你安装额外的情况nginx-module-perl
包(或者配置您的自定义构建的 nginx,如果您正在做的话)。
配置如下:
# 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 https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html了解如何使用 Lua 来实现同样的事情。 Lua 支持需要第三方模块,并且不随 nginx 的默认包一起提供。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)