![Nginx FastCGI 在 stderr 中发送:“主要脚本未知”](https://linux22.com/image/1602204/Nginx%20FastCGI%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)
我正在使用 nixos 和 nginx,我对它们都很陌生。现在我正尝试让 nginx 工作并处理一个 php 文件。每当我尝试打开http://[我的示例].local/在浏览器中,它显示“文件未找到”。nginx-log 指出
Aug 30 19:25:17 nixos nginx[29576]: 2019/08/30 19:25:17 [error] 29579#29579: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: example.local, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/phpfpm/example.sock:", host: "example.local"
配置部分的 nginx 部分内容如下
# Enable nginx web server
services.nginx = {
enable = true;
virtualHosts."example.local" = {
root = "/home/username/PhpstormProjects/wordpress_installations/example/";
locations = {
"~ \.php$".extraConfig = ''
include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/phpfpm/example.sock;
fastcgi_index index.php;
'';
"/".extraConfig = "try_files $uri /index.php?$args ;";
};
};
};
相关的 php 部分是
# phpfpm
services.phpfpm.pools.example = {
listen = "/var/run/phpfpm/example.sock";
extraConfig = ''
pm = ondemand
listen.owner = nginx
pm.max_children=50;
user = username
'';
};
自动生成的 nginx 配置文件是
user nginx nginx;
error_log stderr;
daemon off;
events {
}
http {
include /nix/store/rfs7420034mjxhnwvlyg5cmhddn3q394-nginx-1.14.2/conf/mime.types;
include /nix/store/rfs7420034mjxhnwvlyg5cmhddn3q394-nginx-1.14.2/conf/fastcgi.conf;
include /nix/store/rfs7420034mjxhnwvlyg5cmhddn3q394-nginx-1.14.2/conf/uwsgi_params;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;
# $connection_upgrade is used for websocket proxying
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
client_max_body_size 10m;
server_tokens off;
server {
listen 0.0.0.0:80 ;
listen [::]:80 ;
server_name example.local ;
root /home/username/PhpstormProjects/wordpress_installations/example/;
location / {
try_files $uri /index.php?$args ;
}
location ~ .php$ {
include /nix/store/rfs7420034mjxhnwvlyg5cmhddn3q394-nginx-1.14.2/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/phpfpm/example.sock;
fastcgi_index index.php;
}
}
}