php-fpm/nginx 向脚本提供错误路径信息

php-fpm/nginx 向脚本提供错误路径信息

我使用 nginx 网络服务器,并试图解决网站被赋予错误$_SERVER["PHP_SELF"]变量的问题。这会破坏一些脚本。问题似乎是 nginx 向 FPM 提供了错误的变量。

$_SERVER["PHP_SELF"]是“/index.php/index.php”,应该是“/index.php”

这是 nginx 服务器 PHP 配置...

server {
        listen 192.168.0.3:80;
        server_name cacti;
        access_log /home/admin/cacti_access_log;
        error_log /home/admin/cacti_error_log;
        root /var/www/sites/cacti;

        location / {
                index index.php index.html index.htm;
        }

        location ~* \.php$ {
                fastcgi_pass unix:/var/run/php-fpm/admin-socket;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;

                include fastcgi_params;
                fastcgi_read_timeout 60;
        }
}

fastcgi_参数...

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

答案1

首先,移到条目include fastcgi_params之前fastcgi_param。nginx 按顺序处理这些指令,因此fastcgi_params文件内的配置将覆盖块中的定义server

然后,对PATH_INFO和使用这些设置PATH_TRANSLATED

fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_script_name;

相关内容