![nginx:FastCGI 在 stderr 中发送:“主要脚本未知”](https://linux22.com/image/683299/nginx%EF%BC%9AFastCGI%20%E5%9C%A8%20stderr%20%E4%B8%AD%E5%8F%91%E9%80%81%EF%BC%9A%E2%80%9C%E4%B8%BB%E8%A6%81%E8%84%9A%E6%9C%AC%E6%9C%AA%E7%9F%A5%E2%80%9D.png)
在 64 位 arch linux 上使用最新版本的 nginx(1.10.0)和 php-fpm(PHP 7.0.6)。
当尝试请求 index.php 以进行 DokuWiki 安装时,我收到以下错误:
2016/05/21 22:09:50 [error] 11099#11099: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.70.3, server: doku.test.com, request: "GET /install.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "doku.test.com"
以下是相关的服务器配置:
server {
listen 80;
server_name doku.test.com;
root /var/www/doku/public_html/;
access_log /var/log/nginx/scripts.log scripts;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
这是 fastcgi_params:
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 REQUEST_SCHEME $scheme;
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;
从我的服务器配置中可以看出,我试图记录SCRIPT_FILENAME
参数的输出,因为我的所有研究似乎都指向这是罪魁祸首。以下是 nginx.conf 的相关部分:
log_format scripts '$document_root$fastcgi_script_name > $request';
当请求index.php页面时,会生成以下内容scripts.log
:
/var/www/doku/public_html/index.php > GET /index.php HTTP/1.1
对该文件执行 ls:
-rwxr-xr-x 1 nginx nginx 182 May 21 06:45 /var/www/doku/public_html/index.php
值得注意的是,nginx 守护程序和 php-fpm 守护程序都配置为使用 nginx 组以 nginx 用户身份运行。我不知道为什么我会收到初始错误,因为日志记录有效地证明它SCRIPT_FILENAME
确实指向正确的路径。
在我查看过的所有 ServerFault 答案中,将该参数添加到服务器配置似乎是解决我的错误的最佳方法,但在我看来它似乎并不能解决问题。
有什么建议么?
答案1
首先检查您是否在www.conf
文件中为 PHP-FPM (FastCGI 进程管理器) 设置了正确的监听地址。因此,打开www.conf
文件 (location: /etc/php5/fpm/pool.d/www.conf
),它应该会看到类似以下内容:
Start a new pool named 'www'.
[www]
;prefix = /path/to/pools/$pool
user = www-data
group = www-data
listen = 127.0.0.1:8080
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
在监听地址下,检查是否设置了环回地址(它标识您的服务器)和端口号。然后在文件中nginx.conf
更改fastcgi_pass
为127.0.0.1:8080
,因此最终它应该看起来像:
server {
listen 80;
server_name doku.test.com;
root /var/www/doku/public_html/;
access_log /var/log/nginx/scripts.log scripts;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:8080;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
注意我在示例中使用了端口 8080,您可能需要更改它。