Nginx虚拟主机访问/backend报错:FastCGI 'Primary script unknown'求助
Hey there, let's troubleshoot that frustrating FastCGI error you're seeing when accessing https://website1.inov.com/backend. The "Primary script unknown" message almost always means Nginx can't correctly resolve the PHP script path to pass to PHP-FPM, and I spot the root issue in your alias and FastCGI configuration.
The Core Problem
When using alias in an Nginx location block, the way $document_root and $fastcgi_script_name interact breaks the file path. Here's why:
- Your
location /backendusesalias /var/www/backend, which maps requests to/backend/*to files in/var/www/backend/*. - But your inner PHP location uses
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name. For a request like/backend/index.php, this resolves to/var/www/backend/backend/index.php—a path that doesn't exist (your actual script is at/var/www/backend/index.php).
Fixed Nginx Configuration
Update your /backend location block to use $request_filename instead, which automatically handles the alias path correctly:
location /backend { alias /var/www/backend; index index.php; try_files $uri $uri/ /backend/index.php; location ~ \.php$ { # Prevent passing non-existent scripts to PHP-FPM try_files $uri =404; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # Critical fix: use $request_filename for alias paths fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_index index.php; fastcgi_param CI_ENV production; fastcgi_param DOMAIN production; include fastcgi.conf; } }
Additional Checks to Ensure Success
- File Permissions: Verify that the
/var/www/backenddirectory and its PHP files are readable by the PHP-FPM process user (usuallywww-data). Runsudo chown -R www-data:www-data /var/www/backendif needed. - Socket Access: Confirm the
php7.2-fpm.sockfile exists at/var/run/php/php7.2-fpm.sockand that Nginx has permission to access it (check group ownership—oftenwww-dataorphp-fpm). - Restart Services: Apply the changes by restarting Nginx and PHP-FPM:
sudo systemctl restart nginx sudo systemctl restart php7.2-fpm
After making these adjustments, your /backend path should correctly route to the PHP application without the FastCGI error.
内容的提问来源于stack exchange,提问作者ELIZABETHHHHHH

