nginx怎么获取php的变量

在 Nginx 中获取 PHP 的变量,可以通过两种方式实现。

使用 ngx_http_fastcgi_module 模块的 fastcgi_param 指令:

在 Nginx 的配置文件中,使用 fastcgi_param 指令将 PHP 的变量传递给 FastCGI 进程。例如:

location / {
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VAR $php_var;
    include fastcgi_params;
}

在 PHP 脚本中,可以使用 $_SERVER['PHP_VAR'] 来获取该变量的值。

使用 ngx_http_rewrite_module 模块的 set 指令:

在 Nginx 的配置文件中,使用 set 指令将 PHP 的变量设置为 Nginx 的变量。例如:

location / {
    set $php_var $arg_php_var;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VAR $php_var;
    include fastcgi_params;
}

在 PHP 脚本中,可以使用 $_SERVER['PHP_VAR'] 来获取该变量的值。

需要注意的是,以上方法都是在 Nginx 配置层面传递 PHP 变量,而不是直接从 PHP 脚本中获取。

阅读剩余
THE END