Nginx调用PHP-FPM出现「Primary script unknown」错误的排查求助
Nginx调用PHP-FPM出现「Primary script unknown」错误的排查求助
各位大佬好,我最近在配置Nginx+PHP-FPM的时候碰到了一个棘手的问题——请求/api/user.php时总是返回404,同时Nginx错误日志里一直报「Primary script unknown」,折腾了好一阵没找到根源,想请大家帮忙排查一下问题所在!
Nginx错误日志(error.log)内容:
[error] 10910#10910: *7 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /api/user.php HTTP/2.0", upstream: "fastcgi://unix:/run/php/php8.1-fpm.sock:", host: "localhost"
Nginx访问日志(access.log)内容:
127.0.0.1 - - [10/Jun/2023:15:57:33 +0200] "GET /api/user.php HTTP/2.0" 404 16 "-" "curl/7.88.1"
我的Nginx配置文件内容:
pid /path/to/my/directory/nginx.pid; user seba www-data; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 1024; } http { # HTTP server server { listen 8080; server_name syskeep; # Redirect all HTTP traffic to HTTPS return 301 https://$host$request_uri; } # HTTPS server server { listen 443 ssl http2; # Uncomment to let SysKeep public available # listen [::]:443 ssl http2; server_name localhost; root /path/to/my/directory/public/; index index.html index.php; try_files $uri $uri/ =404; # SSL certificate files ssl_certificate /path/to/my/directory/localhost.pem; ssl_certificate_key /path/to/my/directory/localhost-key.pem; # SSL configuration ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; # Add security-related headers add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options nosniff; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header Referrer-Policy "strict-origin-when-cross-origin"; location ^~ /api { alias /path/to/my/directory/core/src/public/; try_files $uri =404; fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } } types { application/javascript js; text/css css; text/html html; } }
测试结果:
我用curl测试接口时,执行命令:
curl https://localhost/api/user.php
得到的响应体是:
File not found.
同时返回的HTTP状态码是404。
另外补充一下,我启动Nginx使用的是自定义配置文件,命令是:
nginx -c /path/to/custom/nginx.conf
麻烦各位帮忙看看哪里出问题了,谢谢!
备注:内容来源于stack exchange,提问作者user18545619
相关产品推荐
相关产品推荐

