93

I use SetEnv in Apache to set some variables in virtualhosts that I recover in PHP using $_SERVER[the_variable].

Now I am switching to Perl Catalyst and Nginx, but it seems that the "env" directive in Nginx is not the same. It does not work. How can it be accomplished?

Here is the background picture, just in case someone can suggest a better approach or my previous system does not work with Nginx.

  • I use the same app for many domains. All data comes from different databases with the same structure.
  • The database name is hardcoded to the virtual host, in that environmental variable.
  • As I know the database name, all the queries go to its appropriate database, from the very first query.
  • I can have multiple domains using the same database, just including the same variable into the directives.
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Nacho B
  • 1,573
  • 1
  • 12
  • 16

4 Answers4

132
location / {
...
   fastcgi_param   APPLICATION_ENV  production;
   fastcgi_param   APPLICATION_CONFIG user;
...
}

but it's for PHP-CGI

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
TREx
  • 1,329
  • 2
  • 7
  • 2
64

NGINX doesn't manage your backend processes like apache does, so it can't affect their environments. To set a new $_SERVER PHP variable from NGINX, you need to add a new fastcgi_param entry along with the rest of them. Wherever you're including fastcgi_params or fastcgi.conf.

7ochem
  • 2,183
  • 1
  • 34
  • 42
kolbyjack
  • 17,660
  • 5
  • 48
  • 35
  • 1
    Thank you, it does the trick. For people using Perl Catalyst, the required value is in $c->engine->env->{MY_CUSTOM_VARIABLE}, similar to $_SERVER in php. [link]http://search.cpan.org/dist/Catalyst-Runtime/lib/Catalyst/Engine.pm – Nacho B Nov 12 '11 at 10:58
  • This is not truth for me... When I set the variable like this, I can retrieve the variable only through getenv method. The variable isn't added to the $_SERVER array. – Pablo Ezequiel Leone Jun 08 '17 at 09:46
  • @MIguelele , variable chain $c->engine->env->{SOME_ENV_VAR} is strictly the same as $ENV{SOME_ENV_VAR} that is distributed by fastcgi interface. – Znik Feb 14 '19 at 11:15
47

You should keep in mind, that nginx doesn't manage php processes like apache does. You should config either php-fpm, or php-cgi, relying what runs php on your server.

php-cgi

...
env[APP_ENV] = production
...

php-fpm

location / {
    ...
    fastcgi_param APP_ENV production; 
    ...
}
Tim Brayshaw
  • 482
  • 3
  • 9
Oleg
  • 7,070
  • 4
  • 47
  • 49
13

The fastcgi_pass socket location needs to come first, then each of the fastcgi_param parameters. You can also list variables in a file in the nginx config folder, then include that file. The include file commonly has the name fastcgi_params. Your environment parameters can be added easily to the php handling block:

        location ~ \.php$ {
            fastcgi_pass     unix:/your_sock_location/nginxFastCGI.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param    APP_ENV production;
            include          fastcgi_params;
        }

The fastcgi_params file located in the same directory as nginx.conf often looks like this:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
i_a
  • 2,735
  • 1
  • 22
  • 21