共計 709 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 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']
來獲取該變量的值。
- 在 Nginx 的配置文件中,使用
-
使用 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 的配置文件中,使用
需要注意的是,以上方法都是在 Nginx 配置層面傳遞 PHP 變量,而不是直接從 PHP 腳本中獲取。
丸趣 TV 網 – 提供最優質的資源集合!
正文完