Access Denied on Nginx and PHP: Diagnosing the FastCGI Gap
Seeing "Welcome to Nginx!" while hitting an "Access Denied" error on a specific .php file is a classic symptom of a configuration mismatch between the web server and the PHP process manager. You have successfully configured Nginx to act as a reverse proxy, but the handshake with PHP-FPM is failing before any logic can execute. The issue rarely lies in Nginx's ability to serve static files; it almost always stems from how the location block handles the request or how PHP-FPM responds to that specific socket connection.
In your configuration, you have enabled the FastCGI pass for .php files via the Unix socket /var/run/php5-fpm.sock. However, the "Access Denied" message usually indicates one of two things: either Nginx is not reaching PHP-FPM at all (returning a 403 or 502 internally), or PHP-FPM has rejected the connection due to permissions or resource constraints.
The FastCGI Socket Path Mismatch
A frequent oversight involves the listen directive in your PHP-FPM configuration versus the fastcgi_pass directive in Nginx. You mentioned enabling listen = /var/run/php5-fpm.sock in /etc/php-fpm.d/www.conf. While this looks correct, it is crucial to verify that the actual socket file exists at that exact path and that it is owned by the correct user groups (usually www-data or nginx).
If the socket is missing or has restrictive permissions (e.g., mode 0600 instead of 0770), Nginx cannot connect. When this happens, Nginx often defaults to returning an error page or a generic "Access Denied" if it cannot resolve the upstream. Ensure that after restarting PHP-FPM, you can list the socket:
ls -l /var/run/php5-fpm.sock
If the file is missing, check the PHP-FPM error log located at /var/log/php5-fpm.log or /var/log/syslog. Common errors there include "bind() to /var/run/php5-fpm.sock failed" which points directly to a permissions issue on the parent directory of the socket.
SCRIPT_FILENAME and Request Handling
Your Nginx configuration includes:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
The line fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; is generally safe, but it can cause issues if your Nginx server block sets a specific root that differs from the actual filesystem path of your application. If your application lives in /var/www/myapp, but your Nginx location or server block sets root /usr/share/nginx/html, the resulting SCRIPT_FILENAME will be incorrect. PHP-FPM will fail to load the script because it is looking for a file that doesn't exist on the filesystem.
Ensure that the root directive in your Nginx configuration matches the physical directory containing your PHP files. If you are using a symbolic link or a specific application root, make sure $document_root resolves correctly within the context of the request.
Security and Extension Limits
You mentioned activating security.limit_extensions = .php .php3 .php4 .php5 .html in /etc/php-fpm.d/www.conf. This directive is valid for PHP 7+ but might cause confusion if your environment uses older PHP versions or a different security configuration structure. More importantly, mixing extension limits with Nginx's fastcgi_param can lead to unexpected behavior if the script name doesn't match an allowed extension.
Additionally, ensure that you haven't inadvertently blocked access to the .htaccess files in Nginx while expecting them to control PHP permissions. The line location ~ /\.ht { deny all; } is correct for security, but remember that Apache's .htaccess directives are ignored entirely when running behind Nginx. Permission handling must happen at the filesystem level (via Unix file modes and ownership) or within the PHP code itself.
Troubleshooting Steps
If the configuration looks correct but the error persists:
- Check Nginx Logs: Look at
/var/log/nginx/error.log. You will likely see messages like "connect() failed (111: Connection refused)" if the socket isn't there, or "upstream timed out" if PHP-FPM is overloaded. - Verify PHP-FPM Status: Run
systemctl status php5-fpm(orphp-fpmdepending on your distro) to ensure the service is actually running and not in a failed state. - Test with a Simple Script: Create a file named
test.phpcontaining<?php phpinfo(); ?>. Place it directly in your document root. If this returns the "Access Denied" error, the issue is almost certainly the FastCGI connection or theSCRIPT_FILENAMEpath.
By aligning the socket paths, verifying filesystem roots, and ensuring PHP-FPM permissions are correctly set, you can resolve the "Access Denied" state and allow your application to execute PHP scripts seamlessly behind Nginx. For more complex routing scenarios involving Laravel applications specifically, ensure that your virtual host configuration points to the correct public directory defined in public/ rather than the project root, a common pitfall when migrating from Apache to Nginx setups.