10

I am starting a project that uses environment variables to set the database connection and a couple other things. (They didn't want to use configuration files since people are careless and overwrite them).

Anyway, I am using nginx and while it supports env - it doesn't seem to support it well enough. You can't set the env values on a per-server block basis. In other words, this won't work:

server {
    listen 80;
    server_name domain;
    env FOO = "bar";
}

You must do this:

env FOO = "bar";

http {
    server {
        listen 80;
        server_name domain;
    }
}

Which means that I can't have vhost-specific values. So I must create a whole vhost config for each site and only activate the one I want at the moment so that the value is set correctly.

Is there any way to work around this?

Xeoncross
  • 4,449
  • 12
  • 43
  • 56
  • Isn't the nginx config just as susceptible to overwriting as the application's config? What module is it using to feed requests to the application? – Shane Madden Jan 19 '12 at 22:36
  • @ShaneMadden, the nginx config isn't part of the project but must be created on each server manually. So it won't be overwritten. What I posted isn' the full configs - we are also just using standard `fastcgi_pass` to forward the request to ruby/php. – Xeoncross Jan 19 '12 at 22:40

1 Answers1

16

It turns out that if you are using fastcgi you can get around this by passing the values from fastcgi_param.

server {
    listen 80;
    server_name domain;

    # Pass PHP scripts to php-fastcgi listening on port 9000
    location ~ path/to/it {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param FOO "bar";
    }
}
Xeoncross
  • 4,449
  • 12
  • 43
  • 56
  • Yup, that's what I was about to suggest. Beat me to it! (I'm out of votes for today, I'll upvote this answer in a bit) – Shane Madden Jan 19 '12 at 23:01
  • @ShaneMadden perhaps you could help [me with this](http://serverfault.com/questions/351710/how-do-you-conditionally-include-files-in-nginx-vhost). – Xeoncross Jan 19 '12 at 23:12
  • 2
    Is there any way to set `server_name` dynamically, though? Since it's a virtual host running on OS X, the nginx host has a different IP, so it doesn't resolve. – taco Sep 18 '15 at 03:32