views:

355

answers:

1

My Rails app makes use of ImageMagick, but the app fails on trying to execute the ImageMagick command ("identify"). I fixed this issue in development (where I'm running Apache/Passenger) by passing the following environment variables in my Apache config:

  SetEnv MAGICK_HOME /opt/local/var/macports/software/ImageMagick/6.5.9-0_0+q16
  SetEnv DYLD_LIBRARY_PATH /opt/local/var/macports/software/ImageMagick/6.5.9-0_0+q16/opt/local/lib
  SetEnv PATH /usr/bin:/opt/local/var/macports/software/ImageMagick/6.5.9-0_0+q16/opt/local/bin

However, my production environment is running Nginx and Mongrel (not something I set up), and I am not sure how to pass those variables to the app. My nginx.conf file currently is as follows:

# user and group to run as
user  mouthbreather mouthbreather;

worker_processes  4;

# pid of nginx master process
pid /var/run/nginx.pid;

events {
  worker_connections  8192;
  use epoll;
}

http {

  include /etc/nginx/mime.types;

  default_type  application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/engineyard/nginx/access.log  main;
  error_log  /var/log/engineyard/nginx/error.log notice;

  sendfile on;

  tcp_nopush        on;
  tcp_nodelay       on;

  gzip              on;
  gzip_http_version 1.0;
  gzip_comp_level   2;
  gzip_proxied      any;
  gzip_buffers      16 8k;
  gzip_types        text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  include /etc/nginx/sites/*.conf;
}

So my questions are:

  1. How can I determine where my MAGICK_HOME is on production?
  2. How can I pass those variables to the app via nginx.conf ?

Thanks!

A: 

In short, you don't pass anything via environment variables with nginx, you use HTTP headers or fastcgi parameters.

In your case, you don't even need to, because you are doing things properly and running mongrels as a separate process - set the environment variables in THEIR environment. Nginx properly has nothing to do with it.

Nginx does not work with environment variables and probably will not for a very long time until someone mistakenly hacks a third party module together for it, and then it still won't be supported in the main stream.

There are a multitude of reasons for this, mostly design, security, administration related, but ultimately it's opinion of the developer and the community that dealing with environment variables is not the place of the HTTPd when the HTTPd is designed to work with resources that might not be on the same machine (how do you pass environment variables to processes listening on another machine nearby?).

Furthermore, passenger is a third party module and is somewhat broken in terms of both the implementation and the design, in that it is against what nginx is designed for because it runs the application processes within nginx (here you COULD pass environment variables in theory, but it's not the way nginx is meant to work).

The recommended method to handle things such as this is to start your application outside of nginx (making use of environment variables there if you wish) and then either proxy or fastcgi pass to your application, optionally including in headers or fastcgi params the necessary extra data. Alternatively, your application may have some way to determine proper settings within it, such as a settings.local file (this is fairly common in python setups).

There are tons of ways to do this without having nginx deal with environment variables.

Anonymous