You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx虚拟主机访问/backend报错:FastCGI 'Primary script unknown'求助

Fixing "Primary script unknown" FastCGI Error for /backend Path

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 /backend uses alias /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/backend directory and its PHP files are readable by the PHP-FPM process user (usually www-data). Run sudo chown -R www-data:www-data /var/www/backend if needed.
  • Socket Access: Confirm the php7.2-fpm.sock file exists at /var/run/php/php7.2-fpm.sock and that Nginx has permission to access it (check group ownership—often www-data or php-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:30:46