Nginx + PHP FPM : PATH_INFO always empty

标签 nginx fastcgi php

我在 Debian 上配置了 nginx stable (1.4.4) + PHP(使用 FastCGI、php-fpm)。效果很好:

     location ~* ^/~(.+?)(/.*\.php)$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        alias /home/$1/public_html$2;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_index index.php;
        autoindex on;
     }

我使用 PATH_INFO 变量,因此我将以下行添加到 fastcgi_params:

fastcgi_param   PATH_INFO       $fastcgi_path_info;

在/etc/php5/fpm/php.ini 中:

cgi.fix_pathinfo = 0

我认为这应该可行,但是当我打印出所有服务器变量时,PATH_INFO 始终为空:

    array (
  'USER' => 'www-data',
  'HOME' => '/var/www',
  'FCGI_ROLE' => 'RESPONDER',
  'QUERY_STRING' => '',
  'REQUEST_METHOD' => 'GET',
  'CONTENT_TYPE' => '',
  'CONTENT_LENGTH' => '',
  'SCRIPT_FILENAME' => '/usr/share/nginx/html/srv_var.php',
  'SCRIPT_NAME' => '/srv_var.php',
  'PATH_INFO' => '',
  'REQUEST_URI' => '/srv_var.php',
  'DOCUMENT_URI' => '/srv_var.php',
  'DOCUMENT_ROOT' => '/usr/share/nginx/html',
  'SERVER_PROTOCOL' => 'HTTP/1.1',
  'GATEWAY_INTERFACE' => 'CGI/1.1',
  'SERVER_SOFTWARE' => 'nginx/1.4.4',
  .....
)

我想不出问题出在哪里。有什么想法吗?

最佳答案

我偶然发现了一个解决方案。 $fastcgi_path_info var 与 $fastcgi_split_path_info 一起工作,需要在 location block 中设置。以下是在我们的环境中有效的方法:

location ~ [^/]\.php(/|$) {
    root /var/www/jurism-php;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
}

Nginx documentation 中也有一个示例在 fastcgi_split_path_info 下。

(...我现在看到它匹配上面的不止一篇文章。可能需要在 include 语句之后设置 PATH_INFO 行,以避免破坏值。)

关于Nginx + PHP FPM : PATH_INFO always empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848899/

相关文章:

ssl - 本地主机上的 Nginx SSL

nginx - php5-fpm崩溃

php - 从 mysql 表中选择最新元素

FCGI 和 PSGI 之间的 Perl 基准测试

php - 由于 PHP-FPM 监听队列,站点无法访问,CPU 达到 100%

javascript - 获取 mysql 表中的图像以显示在 javascript 幻灯片中

php - $(document).ready(function(){ 每次在 Hyperlink 的 onclick 事件上执行?

nginx - 我真的需要 Web 服务器和 API - 微服务架构中的网关吗

Nginx 位置/vs/artifactory

php - 如何在 nginx (Ubuntu 16.04) 上配置基本的 FastCGI 缓存