简体   繁体   English

使用 Nginx 运行 PHP 时找不到文件

[英]File Not Found when running PHP with Nginx

Recently I installed the latest version of Nginx and looks like I'm having hard time running PHP with it.最近我安装了最新版本的 Nginx,看起来我很难用它运行 PHP。

Here is the configuration file I'm using for the domain:这是我用于域的配置文件:

server {
listen       80;
server_name  localhost;

location / {
    root   /usr/share/nginx/html;
    index  index.php;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

} }

Here is the error I'm getting on the error log file:这是我在错误日志文件中遇到的错误:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

尝试另一个 *fastcgi_param* 类似的东西

fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

I had the "file not found" problem, so I moved the "root" definition up into the "server" bracket to provide a default value for all the locations.我遇到了“找不到文件”的问题,因此我将“根”定义移到了“服务器”括号中,以为所有位置提供默认值。 You can always override this by giving any location it's own root.你总是可以通过给任何位置它自己的根来覆盖它。

server {
    root /usr/share/nginx/www;
    location / {
            #root /usr/share/nginx/www;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

Alternatively, I could have defined root in both my locations.或者,我可以在我的两个位置都定义 root。

Probably it's too late to answer but a couple things since this is a really annoying error.可能现在回答为时已晚,但有几件事因为这是一个非常烦人的错误。 Following solution worked on Mac OS X Yosemite.以下解决方案适用于 Mac OS X Yosemite。

  1. It's the best if you have如果你有那就最好了

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  1. The include with fast cgi params should go above that line.带有快速 cgi 参数的包含应该在该行之上。

  2. All your directories down to the PHP file you're executing (including that file too) should have a+x permissions, eg您正在执行的 PHP 文件(也包括该文件)的所有目录都应该具有a+x权限,例如

sudo chmod a+x /Users/ sudo chmod a+x /Users/oleg/ sudo chmod a+x /Users/oleg/www/ sudo chmod a+x /Users/oleg/www/a.php

I had been having the same issues, And during my tests, I have faced both problems:我一直有同样的问题,在我的测试中,我遇到了两个问题:

1º: "File not found" 1º:“找不到文件”

and

2º: 404 Error page 2º:404 错误页面

And I found out that, in my case:我发现,就我而言:

I had to mount volumes for my public folders both on the Nginx volumes and the PHP volumes.我必须在 Nginx 卷和 PHP 卷上为我的公共文件夹安装卷。

If it's mounted in Nginx and is not mounted in PHP , it will give: " File not found "如果它安装在Nginx 中并且没有安装在PHP 中,它会给出:“找不到文件

Examples (Will show "File not found error"):示例(将显示“找不到文件错误”):


services:
  php-fpm:
    build:
      context: ./docker/php-fpm
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

If it's mounted in PHP and is not mounted in Nginx , it will give a 404 Page Not Found error .如果它安装在PHP 中并且未安装在Nginx 中,它将给出404 Page Not Found 错误

Example (Will throw 404 Page Not Found Error):示例(会抛出 404 Page Not Found 错误):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

And this would work just fine (mounting on both sides) (Assuming everything else is well configured and you're facing the same problem as me):这将工作得很好(安装在两侧) (假设其他一切都配置良好并且您面临与我相同的问题):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      # Mount PHP for Public Folder
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:

        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

Also here's a Full working example project using Nginx/Php, for serving multiple sites: https://github.com/Pablo-Camara/simple-multi-site-docker-compose-nginx-alpine-php-fpm-alpine-https-ssl-certificates这里还有一个使用 Nginx/Php 的完整工作示例项目,用于为多个站点提供服务: https : //github.com/Pablo-Camara/simple-multi-site-docker-compose-nginx-alpine-php-fpm-alpine-https -ssl-证书

I hope this helps someone, And if anyone knows more about this please let me know, Thanks!我希望这对某人有所帮助,如果有人对此了解更多,请告诉我,谢谢!

I just spent like 40 minutes trying to debug a non-working /status with:我只花了大约 40 分钟试图调试一个非工作的 /status :

$ SCRIPT_NAME=/status SCRIPT_FILENAME=/status QUERY_STRING= REQUEST_METHOD=GET cgi-fcgi -bind -connect /var/run/php5-fpm.sock

It just produced "File not found" error, while the actual scripts (that are found on the filesystem) worked just fine.它只是产生了“找不到文件”错误,而实际的脚本(在文件系统上找到的)工作得很好。

Turned out, I had a couple of orphaned processes of php5-fpm.原来,我有几个孤立的 php5-fpm 进程。 After I killed everything and restarted php5-fpm cleanly, it just went back to normal.在我杀死所有内容并干净地重新启动 php5-fpm 后,它又恢复了正常。

Hope this helps.希望这可以帮助。

In my case the PHP-script itself returned 404 code.就我而言,PHP 脚本本身返回 404 代码。 Had nothing to do with nginx.与nginx无关。

In my case, it was because the permissions on the root web directory were not set correctly.就我而言,这是因为根 Web 目录的权限设置不正确。 To do this, you need to be in the parent folder when you run this in terminal:为此,当您在终端中运行它时,您需要位于父文件夹中:

sudo chmod -R 755 htmlfoldername

This will chmod all files in your html folder, which is not recommended for production for security reasons, but should let you see the files in that folder, to be sure that isn't the issue while troubleshooting.这将 chmod 您的 html 文件夹中的所有文件,出于安全原因,不建议将其用于生产,但应该让您看到该文件夹​​中的文件,以确保这不是故障排除时的问题。

The error message “Primary script unknown or in your case is file not found.”错误消息“主脚本未知或在您的情况下未找到文件。” is almost always related to a wrongly set in line SCRIPT_FILENAME in the Nginx fastcgi_param directive (Quote from https://serverfault.com/a/517327/560171 ).几乎总是与 Nginx fastcgi_param 指令中的 SCRIPT_FILENAME 行错误设置有关(引自https://serverfault.com/a/517327/560171 )。

In my case, I use Nginx 1.17.10 and my configuration is:就我而言,我使用 Nginx 1.17.10,我的配置是:

    location ~ \.php$ {
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_read_timeout 600;
    }

You just change $document_root to $realpath_root, so whatever your root location, like /var/www/html/project/ , you don't need write fastcgi_param SCRIPT_FILENAME /var/www/html/project$fastcgi_script_name;您只需将 $document_root 更改为 $realpath_root,因此无论您的根位置如何,例如/var/www/html/project/ ,您都不需要编写fastcgi_param SCRIPT_FILENAME /var/www/html/project$fastcgi_script_name; each time your root is changes.每次你的根改变。 This configuration is more flexible.这种配置更加灵活。 May this helps.可能这会有所帮助。

================================================================================= ================================================== ================================

For more information, if you got unix:/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream , just change in /etc/nginx/nginx.conf , from user nginx;有关更多信息,如果您unix:/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream遇到unix:/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream ,只需从user nginx;更改/etc/nginx/nginx.conf user nginx; to user www-data;user www-data; . .

Because the default user and group of PHP-FPM process is www-data as can be seen in /etc/php/7.2/fpm/pool.d/www.conf file (Qoute from https://www.linuxbabe.com/ubuntu/install-nginx-latest-version-ubuntu-18-04 ):因为 PHP-FPM 进程的默认用户和组是 www-data,可以在/etc/php/7.2/fpm/pool.d/www.conf文件中看到(来自https://www.linuxbabe.com/ ubuntu/install-nginx-latest-version-ubuntu-18-04 ):

user = www-data
group = www-data

May this information gives a big help可能这些信息有很大帮助

I had this error as well.我也有这个错误。 In my case it was because there was another virtual host that was pointing to the same root directory.就我而言,这是因为有另一个虚拟主机指向同一个根目录。

After upgrading to PHP72, we had an issue where the php-fpm.d/www.conf lost the settings for user/group which was causing this error.升级到 PHP72 后,我们遇到了一个问题,即 php-fpm.d/www.conf 丢失了导致此错误的用户/组设置。 Be sure to double check those if your setup involves php-fpm.如果您的设置涉及 php-fpm,请务必仔细检查这些。

For me, problem was Typo in location path.对我来说,问题是位置路径中的错字。

Maybe first thing to check out for this kind of problem也许首先要检查这种问题

Is path to project.是项目的路径。

当收到“找不到文件”时,我的问题是在 ngix 配置中指向此行的文件夹中没有符号链接:

root /var/www/claims/web;

in case it helps someone, my issue seems to be just because I was using a subfolder under my home directory, even though permissions seem correct and I don't have SELinux or anything like that.万一它对某人有帮助,我的问题似乎只是因为我在我的主目录下使用了一个子文件夹,即使权限似乎正确而且我没有 SELinux 或类似的东西。 changing it to be under /var/www/something/something made it work.将其更改为 /var/www/something/something 使其工作。

(if I ever found the real cause, and remember this answer, I'll update it) (如果我找到了真正的原因,并记住了这个答案,我会更新它)

试试这个命令

sudo chmod 755 -R htdocs/

my case: I used relative dir我的情况:我使用了相对目录

       location ~* \.(css|js)\.php$ {

            root ../dolibarr/htdocs;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_index index.php;

             include /etc/nginx/fastcgi_params;
       }

this line did not work too : fastcgi_param SCRIPT_FILENAME /vagrant/www/p1/../p2/htdocs/core/js/lib_head.js.php;这一行也不起作用: fastcgi_param SCRIPT_FILENAME /vagrant/www/p1/../p2/htdocs/core/js/lib_head.js.php;

So I discovered fastcgi_param does not support relative path.所以我发现fastcgi_param不支持相对路径。

This works这有效

fastcgi_param SCRIPT_FILENAME /vagrant/www/p2/htdocs/core/js/lib_head.js.php;

The website will show " File Not Found " error.该网站将显示“找不到文件”错误。

在此处输入图片说明

You should check this configure file: /etc/nginx/nginx.conf (error_log) to get the nginx webservice log location.您应该检查此配置文件:/etc/nginx/nginx.conf (error_log) 以获取 nginx webservice 日志位置。 Then check nginx log: /var/log/nginx/error.log to get the root cause:然后检查 nginx 日志: /var/log/nginx/error.log以获取根本原因:

  • Keyword: FastCGI sent in stderr: "Primary script unknown"关键字:在 stderr 中发送的 FastCGI:“主脚本未知”
  • Root cause: php-fpm service's account: apache doesn't have access permission to open webapps directory根本原因:php-fpm 服务的帐户:apache 没有打开 webapps 目录的访问权限

Step 0. Find your root directory: open /etc/nginx/nginx.conf file and find root keyword步骤 0.找到您的根目录:打开 /etc/nginx/nginx.conf 文件并找到root关键字

Root directory: /var/lib/nginx/webapps/根目录:/var/lib/nginx/webapps/ 在此处输入图片说明

Step 1. Make sure apache account can access the ROOT directory.步骤 1.确保 apache 帐户可以访问 ROOT 目录。

sudo -u apache ls -l /var/lib/nginx/webapps/

Step 2. chmod a+x permission for all ROOT folder步骤 2. chmod a+x 对所有 ROOT 文件夹的权限

sudo chmod a+x /var/
sudo chmod a+x /var/lib/
sudo chmod a+x /var/lib/nginx/
sudo chmod a+x /var/lib/nginx/webapps/

I have solved this issue in nginx version: nginx/1.21.3 PHP 7.4.23 macOS Catalina version 10.15.7我在nginx版本解决了这个问题:nginx/1.21.3 PHP 7.4.23 macOS Catalina version 10.15.7

nano /usr/local/etc/nginx/nginx.conf


location ~ \.php$ {
            root           html;
            include        fastcgi.conf; 
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #Comment bellow Line
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #Add This line
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

For me, this File not found problem was because i started php-fpm as sudo.对我来说,这个文件未找到问题是因为我以 sudo 启动了 php-fpm。 First, stop it as sudo and then start without sudo.首先,以 sudo 身份停止它,然后在没有 sudo 的情况下启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM