2

I want to pass the RAILS_ENV env variable to nginx and use it to set the value for the rails_env directive.

I can read the value of the variable from the environment with LUA module:

location @app {
    set_by_lua $env_rails_env 'return os.getenv("RAILS_ENV")';
    return 200 'rails env is: ${env_rails_env}';
}

When I curl it, I get the correct answer:

[jsosic@workstation ~]$ curl http://localhost:3005/
rails env is: development

But, if I want to use it as a value for nginx directive:

location @app {
    set_by_lua $env_rails_env 'return os.getenv("RAILS_ENV")';
    rails_env         $env_rails_env;
    limit_req         zone=one burst=100;
    passenger_enabled on;
}

I get the following log:

Message from application: '${env_rails_env}' database is not configured.
Available: ["default", "development", "test", "production"] 

Is this even possible?

Jakov Sosic
  • 5,157
  • 3
  • 22
  • 33

1 Answers1

0

After some investigation it seems that passenger_app_env (which rails_env is aliased to) is not accepting the variable and instead treating it as a literal.

https://github.com/phusion/passenger-docker/issues/28

So, instead of $env_rails_env expanding to the content of the $RAILS_ENV read by lua, it's treated as a string $env_rails_env. That's why the log line is reporting database not configured.

Also, per nginx Q & A, variables shouldn't be used in configuration files:

"[Variables] are rather costly compared to plain static configuration. [A] macro expansion and "include" directives should be used [with] e.g. sed + make or any other common template mechanism." http://nginx.org/en/docs/faq/variables_in_config.html

What I ended up is using envsubst(1).

Jakov Sosic
  • 5,157
  • 3
  • 22
  • 33