rails,  nginx,  passenger,  phusion

Using environment variables under Nginx

Rails is heavily using environment variables in its configuration files.

Using environment variables under Nginx

Rails is heavily using environment variables in its configuration files.

While configuring them in development is fairly easy…

For instance if we are using bash shell on OSX we could just add them to ~/.bashrc file:

export VAR_NAME="var_value"

For Nginx configuration is a little more involved; simply because Nginx does not normally run under our user and does not have access to variables defined in .bashrc file.

Nevertheless, configuring environment variables that Passenger process can access is simple. We will have to modify Nginx configuration file (/opt/nginx/conf/nginx.conf in our case):

sudo vi /opt/nginx/conf/nginx.conf

Under the “server {” part we can add passenger_env_var directive:

server {
	...
	passenger_env_var VAR_NAME "var_value";

Don’t forget the ; at the end!

Now Rails should have access to VAR_NAME under:

ENV['VAR_NAME']

Subscribe to The infinite monkey theorem

Get the latest posts delivered right to your inbox