配置userdir时,nginx fastcgi“主脚本未知”


10

我讨厌配置nginx。太复杂了 如何使PHP在用户目录中工作?这是我的nginx.conf的相关部分:

    location ~ ^/~(.+?)(/.*)?$ {
        autoindex on;
        autoindex_exact_size off;
        alias /home/$1/public_html$2;

        location ~ \.php {
            include /etc/nginx/fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
        }
    }

这给了我错误:FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream在nginx错误日志中。

这是我的/etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
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;

我假设nginx不能正确解析脚本名称。我讨厌不得不告诉nginx在文件系统中查找的位置,编写自定义正则表达式以获取应具有的内置功能(或至少具有标准化和记录功能)。


其余的在哪里?即/etc/nginx/fastcgi.conf
迈克尔·汉普顿

@MichaelHampton我刚刚添加了该信息
nnyby 2012年

Answers:



4

当您定义重定向,别名并且您确实在nginx中的站点的conf中使用fastcgi时,可以尝试以下操作以使其起作用:

更改:

位置〜\ .php $ {
...
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
...
}


进入:

位置〜\ .php $ {
...
fastcgi_param SCRIPT_FILENAME $ request_filename;
fastcgi_param REQUEST_URI $ uri?$ args;
...
}

如果这不起作用,请尝试在没有REQUEST_URI的情况下按上述方式使用,因此它将是:

位置〜\ .php $ {
...
fastcgi_param SCRIPT_FILENAME $ request_filename;
...
}


这在OSX Sierra中对我
有用-ajaybc

2

尝试location在userdirs中为PHP脚本使用一个单独的块。这不需要嵌套。

location ~ ^/~([^/]+)/(.+\.php)$ {
    alias /home/$1/public_html/$2;
    include /etc/nginx/fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
}

谢谢你的建议!我现在离我们更近一些,但仍然收到完全相同的错误。
nnyby,2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.