Debugging "FastCGI sent in stderr: Primary script unknown": From Log Analysis to Permission Checks

Dec 06, 2025 · Programming · 10 views · 7.8

Keywords: FastCGI | Nginx | PHP-FPM | debugging | log analysis

Abstract: This article provides a systematic approach to debugging the common "Primary script unknown" error in Nginx and PHP-FPM environments. By configuring PHP-FPM access logs, analyzing Nginx and FastCGI parameter passing, and checking file permissions and paths, it guides developers step-by-step to identify the root cause. With concrete configuration examples, it explains how to enable detailed logging, interpret log information, and offers solutions for common issues, helping to efficiently resolve this challenging server error.

Problem Background and Error Phenomenon

In web server environments based on Nginx and PHP-FPM, developers often encounter the following error message:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream...

This error indicates that the FastCGI handler cannot recognize or execute the passed script, but default error logs often lack specific details, making debugging difficult. For example, even with a simple PHP site and basic Nginx setup, the error may suddenly appear, while standard log files (e.g., /var/log/php-fpm/error.log) might have no relevant entries.

Core Debugging Method: Enabling PHP-FPM Access Logs

To obtain more detailed error information, start by configuring PHP-FPM to log access requests. This can be done by modifying PHP-FPM configuration files, with the following steps:

  1. Edit the PHP-FPM pool configuration file (typically located at /etc/php-fpm.d/www.conf), add or modify this line:
access.log = /var/log/$pool.access.log

This setting creates a separate access log file for each worker pool, recording all incoming FastCGI requests.

<ol start="2">
  • Restart the PHP-FPM service to apply the changes, for example, using the command on Linux systems:
  • systemctl restart php-fpm
    <ol start="3">
  • Attempt to access the problematic PHP page, then check the newly generated log file (e.g., /var/log/www.access.log). Log entries usually include the request method, URI, and response status code, such as:
  • - -  10/Nov/2016:19:02:11 +0000 "GET /app.php" 404

    Log Analysis and Problem Diagnosis

    By analyzing the access logs, you can quickly pinpoint the specific cause of the error:

    Additional Debugging Tips and Configuration Optimization

    Beyond access logs, you can enhance debugging capabilities through the following methods:

    Conclusion and Best Practices

    The key to debugging the "Primary script unknown" error lies in systematically checking logs and configurations. By enabling PHP-FPM access logs, developers can obtain specific request details, distinguishing between Nginx configuration errors and file permission issues. Combining this with other log levels and output capture settings allows for deeper analysis. It is recommended to set up detailed logging in advance when deploying new environments or modifying configurations to quickly address potential problems. Additionally, regularly reviewing file permissions and path settings to ensure PHP-FPM processes have sufficient access rights is an effective measure to prevent such errors.

    Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.