Discussion:
How to set vhost-specific ENV vars for pass to php-fpm/fastcgi?
pgndev
2018-11-28 23:34:02 UTC
Permalink
I run nginx + php-fpm.

I include the following 'secure' config into my shared site config

cat ../sec/params.shared
fastcgi_param DATABASE_HOST "localhost";
fastcgi_param DATABASE_PORT "3306";
fastcgi_param DATABASE_NAME "db_name";
fastcgi_param DATABASE_USER "db_user";
fastcgi_param DATABASE_PASSWORD "db_pass";

It works as intended, setting the ENV vars in PHP env.

I'd like to add some vhost-specific env vars, that ONLY exist & are set for
specific vhost's ENVs

For example, I'd like to set up a conditional add'n of

set $this_token ""
if ($host = 'vhost1.example.com') {
set $this_token "data";
10 fastcgi_param VHOST1_TOKEN $this_token;
}

But when I add this stanza to the main config above, conf check fails

nginx: [emerg] "fastcgi_param" directive is not allowed here in
/etc/nginx/sec/param.shared:10
nginx: configuration file /etc/nginx/nginx.conf test failed

If instead I add

set $this_token ""
if ($host = 'vhost1.example.com') {
set $this_token "data";
}
fastcgi_param VHOST1_TOKEN $this_token;

it passes check

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

In this case the ENV var "VHOST1_TOKEN" is corrrectly defined in the match
case, but it is also *defined*, though null, for any/all other vhosts.

How do I construct this conditional ENV var setting to ONLY set/defined the
vars on host match?
Reinis Rozitis
2018-11-29 07:02:32 UTC
Permalink
In this case the ENV var "VHOST1_TOKEN" is corrrectly defined in the match case, but it is also *defined*, though null, for any/all other vhosts.
How do I construct this conditional ENV var setting to ONLY set/defined the vars on host match?
You can add if_not_empty at the end of the particular fastcgi_param directive:

fastcgi_param VHOST1_TOKEN $this_token if_not_empty;

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_param

rr

Loading...