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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17808787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:30:16  来源:igfitidea点击:

File Not Found when running PHP with Nginx

phpnginx

提问by user2298995

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

回答by deagh

Try another *fastcgi_param* something like

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

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

回答by dogatonic

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。

回答by Oleg Berman

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
  1. 如果你有那就最好了

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.

  2. All your directories down to the PHP file you're executing (including that file too) should have a+xpermissions, e.g.

  1. 带有快速 cgi 参数的包含应该在该行之上。

  2. 您正在执行的 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
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

回答by Pablo Camara

I had been having the same issues, And during my tests, I have faced both problems:

我一直有同样的问题,在我的测试中,我遇到了两个问题:

1o: "File not found"

1o:“未找到文件”

and

2o: 404 Error page

2o: 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 Nginxand 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 PHPand 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!

我希望这对某人有所帮助,如果有人对此了解更多,请告诉我,谢谢!

回答by Alex A.

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. After I killed everything and restarted php5-fpm cleanly, it just went back to normal.

原来,我有几个孤立的 php5-fpm 进程。在我杀死所有内容并干净地重新启动 php5-fpm 后,它又恢复了正常。

Hope this helps.

希望这可以帮助。

回答by rncrtr

In my case, it was because the permissions on the root web directory were not set correctly. To do this, you need to be in the parent folder when you run this in terminal:

就我而言,这是因为根 Web 目录的权限设置不正确。为此,当您在终端中运行它时,您需要位于父文件夹中:

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 文件夹中的所有文件,出于安全原因,不建议将其用于生产,但应该让您看到该文件夹​​中的文件,以确保这不是故障排除时的问题。

回答by PHZ.fi-Pharazon

In my case the PHP-script itself returned 404 code. Had nothing to do with nginx.

就我而言,PHP 脚本本身返回 404 代码。与nginx无关。

回答by Sarcastron

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.

我也有这个错误。就我而言,这是因为有另一个虚拟主机指向同一个根目录。

回答by Aepod

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. Be sure to double check those if your setup involves php-fpm.

升级到 PHP72 后,我们遇到了一个问题,即 php-fpm.d/www.conf 丢失了导致此错误的用户/组设置。如果您的设置涉及 php-fpm,请务必仔细检查这些内容。

回答by Darius.V

When getting "File not found", my problem was that there was no symlink in the folder where was pointing this line in ngix config:

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

root /var/www/claims/web;