NGINX 和配置文件中的环境变量

发布于 2024-10-13 03:21:49 字数 908 浏览 9 评论 0原文

我正在尝试通过 nginx 的配置文件为它设置一些环境变量。我正在使用 nginx/0.8.53 但它不起作用。

server {
    listen 80;
    server_name localdictus;
    root /opt/vdmo_dictus/public;   # <--- be sure to point to 'public'!
    passenger_enabled on;
    rails_env development;
    env VDMO_MANDANT = "somevalue";
    }

这是错误消息:

unknown directive "env" in /opt/nginx/conf/nginx.conf:43

文档告诉我有一个“env”命令...那么我做错了什么? http://wiki.nginx.org/CoreModule#env

通过导出设置环境变量顺便说一句,shell 不适合我的应用程序。

以下是:

37:    server {
38:    listen 80;
39:    server_name localdictus;
40:    root /opt/vdmo_dictus/public;   # <--- be sure to point to 'public'!
41:    passenger_enabled on;
42:    rails_env development;
43:    env VDMO_MANDANT = "somevalue";
44:    }

问候,

亚历克斯

I'm trying to set some environment variables to nginx via it's configuration file. I'm using nginx/0.8.53 and it's not working.

server {
    listen 80;
    server_name localdictus;
    root /opt/vdmo_dictus/public;   # <--- be sure to point to 'public'!
    passenger_enabled on;
    rails_env development;
    env VDMO_MANDANT = "somevalue";
    }

This is the error message:

unknown directive "env" in /opt/nginx/conf/nginx.conf:43

The documentation tells me that there is an "env" command... so what I'm doing wrong ??
http://wiki.nginx.org/CoreModule#env

setting the environment variables via export on the shell is not an option for my application by the way.

Here are the lines:

37:    server {
38:    listen 80;
39:    server_name localdictus;
40:    root /opt/vdmo_dictus/public;   # <--- be sure to point to 'public'!
41:    passenger_enabled on;
42:    rails_env development;
43:    env VDMO_MANDANT = "somevalue";
44:    }

Regards,

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-10-20 03:21:49

链接到 env 指令的“上下文”的文档中是main,而不是server。将指令放在 server { ... } 块之外(任何块之外)。

另请参阅此讨论。我不相信 env 指令可以满足您的要求。

From the documentation you linked to the "Context" for the env directive is main, not server. Put the directive outside of your server { ... } block (outside of any block).

See also this discussion. I do not believe that the env directive does what you are looking for.

堇年纸鸢 2024-10-20 03:21:49

不要传递 env 指令。只需在启动 nginx 时使用 -E 标志即可:

 sudo -E /usr/local/nginx/sbin/nginx

Don't pass the env directive. Just use the -E flag when starting nginx:

 sudo -E /usr/local/nginx/sbin/nginx
预谋 2024-10-20 03:21:49

使用 nginx 为 Rails 应用程序设置环境变量的解决方案。

例如,您的 RAILS_ROOT 是: /opt/myapp_MANDANT

然后以下代码将从 RAILS_ROOT 路径中提取 MANDANT 并将其设置到 Rails 环境中。

split = RAILS_ROOT.split("_")
puts split.inspect


if split.size > 1
    ENV['VDMO_SYSTEM'] = split[2]
    ENV['VDMO_MANDANT'] = split[1]
elsif split.size > 0
    ENV['VDMO_SYSTEM'] = nil
    ENV['VDMO_MANDANT'] = split[1]
end

将此代码放入您的environment.rb 文件中即可工作。

使用此方法的一个好方法是使用 --bind 选项进行挂载。

示例:

   mkdir railsapp_mandant
   mount -t /originalsource /railsapp_mandant

然后将 Rails 应用程序的公共路径设置为 /originalsource/public/ 而不是 /railsapp_mandant/public/

A solution for setting the environment variables for a rails app using nginx.

Your RAILS_ROOT , for example, is : /opt/myapp_MANDANT

Then the following code would extract MANDANT from the RAILS_ROOT path and set in to the rails env.

split = RAILS_ROOT.split("_")
puts split.inspect


if split.size > 1
    ENV['VDMO_SYSTEM'] = split[2]
    ENV['VDMO_MANDANT'] = split[1]
elsif split.size > 0
    ENV['VDMO_SYSTEM'] = nil
    ENV['VDMO_MANDANT'] = split[1]
end

put this code in your environment.rb file to work.

A nice way for using this approch is mount with the --bind option.

Example:

   mkdir railsapp_mandant
   mount -t /originalsource /railsapp_mandant

then set the public path of the rails app to to /originalsource/public/ instead of /railsapp_mandant/public/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文