使用nginx / php-fpm设置$ _ENV(fka $ HTTP_ENV_VARS)

时间:2011-12-18 12:08:07

标签: nginx environment-variables php

apache环境中setenv的等价物是什么? 使用apache,我可以设置env“SOMEE​​NV”并通过$ _ENV ['SOMEE​​NV']在php中访问它 - 但我不知道如何使用nginx + php-fpm。

我最初认为我只需要在php-fpm池的配置中设置ENV [SOMENEV] = test,但var_dump($ _ ENV)仍然不返回任何内容。

任何提示?

2 个答案:

答案 0 :(得分:18)

nginx没有办法影响php的环境,因为它没有将php解释器嵌入到它的进程中。它通过fastcgi_param指令将参数传递给php。您可以在设置其余参数的位置添加一个,并通过$ _SERVER:

访问它
location ~ \.php$ {
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  fastcgi_param SOMEENV test;
  fastcgi_pass php;
}

答案 1 :(得分:6)

请注意$_ENV变量的可用性取决于php-fpm使用的php.ini中variables_order的设置。默认值为EGPCS,其中E为环境,但在Ubuntu 12.04上,我发现它是GPCS。 php.ini本身带有关于$_ENV

的警告
; This directive determines which super global arrays are registered when PHP
; starts up. G,P,C,E & S are abbreviations for the following respective super
; globals: GET, POST, COOKIE, ENV and SERVER. There is a performance penalty
; paid for the registration of these arrays and because ENV is not as commonly
; used as the others, ENV is not recommended on productions servers.

建议使用始终可用的getenv()。我发现我在FPM池中设置的变量可以通过这种方式检索。