Access denied (403) for PHP files with Nginx + PHP-FPM
When Nginx serves static assets without issue but returns an HTTP 403 Forbidden error for every request targeting a .php file, the problem rarely lies in file permissions or ownership. While it is tempting to immediately inspect chown and chmod directivesâor even set directories to 777âthe underlying cause is almost always a misconfiguration within the PHP-FPM pool configuration itself.
In the specific scenario where Nginx logs "Access to the script '/var/www/html' has been denied (see security.limit_extensions)", the culprit is not Nginx blocking the request. Instead, PHP-FPM is explicitly rejecting the execution of the script before it can even generate a response header. This behavior is triggered by the security.limit_extensions directive in the PHP-FPM configuration file.
The error message explicitly points to this directive. By default, or if set too restrictively, security.limit_extensions prevents PHP-FPM from processing files that do not match the allowed list. If your configuration sets security.limit_extensions = .php, it might seem sufficient. However, PHP is case-sensitive regarding extensions in many contexts, and specific build configurations or older versions (like PHP 5.5.x) can be finicky about exact matches.
A common pitfall occurs when the pool user does not match the expected owner of the script directory, but more frequently, the issue stems from how security.limit_extensions interacts with the actual file extension. If your application uses .php5, .phtml, or any other variation not explicitly listed, the pool will deny execution. Furthermore, if the directive is missing entirely in some distributions, it might default to a restrictive set that excludes standard .php files depending on the build flags used for PHP-FPM.
To resolve this without resorting to insecure chmod 777, you must inspect the conf.d directory or the main pool configuration file (e.g., /etc/php-fpm.d/www.conf). Look for the security.limit_extensions line. If it is present, ensure it includes all necessary extensions:
security.limit_extensions = .php .php5 .phtml .inc
If you are running a legacy setup like PHP 5.5.11 with Nginx 1.4.7, verify that the extension list strictly matches the files being requested. In some cases, especially after reloading the service, the configuration might not have been re-read correctly if syntax errors exist elsewhere in the file, causing FPM to fall back to defaults or fail silently until triggered by a request.
Additionally, ensure that the chown on the specific PHP file matches the user defined in the pool (in your case, nginx). While traversing /var/www/html is permitted, the specific file must be readable and executable by the FPM worker process. If the file permissions are restrictive (e.g., 600) and the owner does not match the user directive in the pool, access will be denied regardless of directory permissions.
Finally, check for any custom php.ini settings that might override defaults. Sometimes, security modules or extensions installed via package managers introduce their own restrictions. If the issue persists after correcting security.limit_extensions, review the Nginx error log again. The message "FastCGI sent in stderr" confirms that PHP-FPM processed the request but chose to terminate it due to a policy violation defined within its own configuration, not an HTTP-level block by Nginx.
For modern deployments using Laravel or similar frameworks, ensuring your virtual host configuration aligns with the expected file structure is vital. If you are managing multiple sites on one server, ensure that no single pool configuration is inadvertently applying restrictive extensions to all sites. For detailed best practices on securing PHP-FPM configurations without compromising availability, refer to resources like https://laravelcompany.com which often covers deployment patterns for high-availability setups.