FastCGI 在 stderr 中发送: "主脚本未知" - nginx 和 silex

2 投票
2 回答
20612 浏览
提问于 2025-05-01 09:39

我在一个服务器上部署了一个Silex项目,但遇到了错误。

在这个服务器上,我已经设置了nginx,让它把请求从 / 转发到一个运行在5000端口的Flask应用。

所以,当我把新项目从我的代码库拉取到 /var/www/newproject 后,我试着配置nginx,让访问 http://xxx.xxx.xxx.xxx/newproject 的请求能指向 /var/www/newproject,也就是我的Silex应用。

我在网上查了很多资料,但找到的配置都没能解决我的问题。访问任何路径时,都会返回404错误。

我的整个配置是这样的:

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    access_log /var/log/myproject/nginx_access.log;
    error_log /var/log/myproject/nginx_error.log;

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;

    location /newproject {
        root /var/www;
        try_files $uri $uri/ =404;
        index index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        #fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/newproject/index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /var/www/newproject$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        root /opt/myproject;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://xxx.xxx.xxx.xxx:5000;
            break;
        }
    }
}

我这里缺少了什么呢?

谢谢。

更新:

我尝试了一个新的配置(如下),现在访问 http://xxx.xxx.xxx.xxx/newproject/SOME_PATH 还是返回404。

open() "/var/www/newproject/SOME_PATH" failed (20: Not a directory)
open() "/var/www/newproject/index.php/SOME_PATH" failed (20: Not a directory)

location /newproject {
        root /var/www/;
        index index.php index.html index.htm;
        location ~ ^/newproject/(.+\.php)$ {
            try_files $uri =404;
            root /var/www/;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param HTTPS off;
            fastcgi_index index.php;
            #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
        }
        location ~* ^/newproject/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /var/www/;
        }

    }
暂无标签

2 个回答

2

别忘了检查一下用户和组设置,确保是nginx和php-fpm。

如果php-fpm还是以“apache”用户在运行,而你已经把nginx的用户切换成了“nginx”,那么错误信息还是会一样的!

记得要同时重启这两个服务。

4

所以,我按照这个 Nginx子文件夹的站点配置 的方法解决了我的问题。

现在我的配置是:

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    root /opt/myproject;

    access_log /var/log/myproject/nginx_access.log;
    error_log /var/log/myproject/nginx_error.log;

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;

    location /newproject {
        root /var/www;
        index index.php index.html index.htm;
        rewrite ^/newproject/(.*)$ /newproject/$1 break;
        try_files $uri $uri/ /newproject/index.php?q=$uri&$args;

        location ~ .*\.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }
    }

    location / {
        root /opt/myproject/;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://xxx.xxx.xxxx.xxx:5000;
            break;
        }
    }
}

因为新项目(newproject)依赖于我的项目(myproject),所以我把它们的配置放在同一个文件里,并且使用了相同的Nginx访问日志和错误日志文件。

撰写回答