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

Jos*_*osh 8 php nginx environment-variables

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

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

任何提示?

kol*_*ack 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;
}
Run Code Online (Sandbox Code Playgroud)


shr*_*keh 6

请注意,$_ENV变量的可用性取决于variables_orderphp-fpm使用的php.ini中的设置.默认是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.
Run Code Online (Sandbox Code Playgroud)

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