1

Is it possible with NGINX to pick different enviroment variables depending on the subdomain? And if yes how can I do that?

Background: The environment variables link to a certain Mongo DB instance and I´d like to use multiple mongo DBs depending on the subdomain (for every customer).

Deer Hunter
  • 1,070
  • 7
  • 17
  • 25
Jim Smith
  • 11
  • 1
  • How nginx, mongo and environment variables are connected? – Alexey Ten Nov 12 '14 at 14:20
  • You will need to use thirdparty modules to do this (lua/perl). You can't manipulate environment variables "natively" in nginx configuration. – Xavier Lucas Nov 12 '14 at 14:39
  • Haha, I came here looking for the exact same solution that you're thinking of, right down to a meteor SaaS managed with different mongoDBs. did you ever come up with a solution? – Dave Aug 02 '15 at 04:10

1 Answers1

0

According the official documentation, the only way to manipulate environment variables in nginx is through the use of the env directive, only available in the main context (ie not dependent on the protocol such as http, thus not its inherent servers).

That means, the variables will be set for the whole nginx environment (even though it seems you can change it worker-based, which are independant processes).

To do what you wish, I would suggest either:

  • Use separate nginx instances for each subdomain, reading the common configuration parts from the same files (so you do not have to maintain separate copies of the main trunk) and the set the environment of each nginx instance according to the specific subdomain it serves
  • Follow @XavierLucas's proposal and use some modules such as the embedded perl or the third-party Lua ones
Bernard Rosset
  • 1,323
  • 12
  • 24