nginx file not fund

本文介绍了解决Linux环境下nginx配置不当导致PHP文件出现404错误的方法,包括修正配置文件中的root指令及确保文件权限正确。

最近在Linux上搭建nginx环境成功,php-fpm,nginx启动后,访问html文件正常,访问php文件出现了file not fund。

原因如下:

1.配置错误:

关键部分的的配置如下所示。

location / {
            root   /data;    #nginx的路径
            index  index.html index.htm index.php;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
         }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {                   
            root           /data;   # fasctcgi 寻找php的路径
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  #指定路径
            include         fastcgi_params;
        }

root指令放在location中,只针对当前location有用,对其他的location无用,所以,location / {}和location ~\.php{}的root都要改。

$document_root指的是location定义的root路径。



2.权限错误:

这个很容易忽略。

要看路径中的每个文件的其他用户属性是否有r,x权限,没有请执行 chmod 755 file


[root@yfw ~]# cd /www/wwwroot/yfw.szrengjing.com [root@yfw yfw.szrengjing.com]# ./deploy.sh 🔧 开始自动化部署... fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git No composer.json in current directory, do you want to use the one at /www/wwwroot? [Y,n]? y Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default. Installing dependencies from lock file Verifying lock file contents can be installed on current platform. Nothing to install, update or remove Generating optimized autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! Could not open input file: think Could not open input file: think chown: changing ownership of 'public/.user.ini': Operation not permitted chmod: changing permissions of 'public/.user.ini': Operation not permitted PHP Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 PHP Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Failed to reload php7.4-fpm.service: Unit php7.4-fpm.service not found. 🎉 部署完成 [root@yfw yfw.szrengjing.com]# lsattr /www/wwwroot/yfw.szrengjing.com/public/.user.ini ----i---------e----- /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# chattr -i /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# cd /www/wwwroot [root@yfw wwwroot]# [root@yfw wwwroot]# # 备份旧站点 [root@yfw wwwroot]# mv yfw.szrengjing.com yfw.szrengjing.com.bak [root@yfw wwwroot]# [root@yfw wwwroot]# # 使用 Composer 创建新 TP6 项目 [root@yfw wwwroot]# composer create-project topthink/think yfw.szrengjing.com Creating a "topthink/think" project at "./yfw.szrengjing.com" Cannot use topthink/think's latest version v8.1.3 as it requires php >=8.0.0 which is not satisfied by your platform. Installing topthink/think (v6.1.0) - Downloading topthink/think (v6.1.0) - Installing topthink/think (v6.1.0): Extracting archive Created project in /www/wwwroot/yfw.szrengjing.com Loading composer repositories with package information Updating dependencies Lock file operations: 17 installs, 0 updates, 0 removals - Locking league/flysystem (1.1.10) - Locking league/flysystem-cached-adapter (1.1.0) - Locking league/mime-type-detection (1.16.0) - Locking psr/cache (1.0.1) - Locking psr/container (1.1.2) - Locking psr/http-message (1.1) - Locking psr/log (1.1.4) - Locking psr/simple-cache (1.0.1) - Locking symfony/polyfill-mbstring (v1.33.0) - Locking symfony/polyfill-php72 (v1.31.0) - Locking symfony/polyfill-php80 (v1.33.0) - Locking symfony/var-dumper (v4.4.47) - Locking topthink/framework (v6.1.5) - Locking topthink/think-filesystem (v1.0.3) - Locking topthink/think-helper (v3.1.11) - Locking topthink/think-orm (v2.0.62) - Locking topthink/think-trace (v1.6) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 17 installs, 0 updates, 0 removals - Downloading psr/cache (1.0.1) - Downloading psr/container (1.1.2) - Downloading psr/http-message (1.1) - Downloading symfony/var-dumper (v4.4.47) - Downloading topthink/think-helper (v3.1.11) - Downloading psr/simple-cache (1.0.1) - Downloading psr/log (1.1.4) - Downloading topthink/think-orm (v2.0.62) - Downloading topthink/framework (v6.1.5) - Downloading league/flysystem (1.1.10) - Downloading league/flysystem-cached-adapter (1.1.0) - Downloading topthink/think-filesystem (v1.0.3) - Downloading topthink/think-trace (v1.6) - Installing league/mime-type-detection (1.16.0): Extracting archive - Installing psr/cache (1.0.1): Extracting archive - Installing psr/container (1.1.2): Extracting archive - Installing psr/http-message (1.1): Extracting archive - Installing symfony/polyfill-php80 (v1.33.0): Extracting archive - Installing symfony/polyfill-php72 (v1.31.0) - Installing symfony/polyfill-mbstring (v1.33.0): Extracting archive - Installing symfony/var-dumper (v4.4.47): Extracting archive - Installing topthink/think-helper (v3.1.11): Extracting archive - Installing psr/simple-cache (1.0.1): Extracting archive - Installing psr/log (1.1.4): Extracting archive - Installing topthink/think-orm (v2.0.62): Extracting archive - Installing topthink/framework (v6.1.5): Extracting archive - Installing league/flysystem (1.1.10): Extracting archive - Installing league/flysystem-cached-adapter (1.1.0): Extracting archive - Installing topthink/think-filesystem (v1.0.3): Extracting archive - Installing topthink/think-trace (v1.6): Extracting archive 12 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files > @php think service:discover Succeed! > @php think vendor:publish Succeed! 6 packages you are using are looking for funding. Use the `composer fund` command to find out more! Found 1 security vulnerability advisory affecting 1 package. Run "composer audit" for a full list of advisories. [root@yfw wwwroot]# ls -la /www/wwwroot/yfw.szrengjing.com total 108 drwxr-xr-x 10 root root 4096 Nov 14 17:05 . drwxr-xr-x 13 www www 4096 Nov 14 17:05 .. drwxr-xr-x 3 root root 4096 Oct 24 2022 app -rw-r--r-- 1 root root 1138 Oct 24 2022 composer.json -rw-r--r-- 1 root root 37667 Nov 14 17:05 composer.lock drwxr-xr-x 2 root root 4096 Nov 14 17:05 config -rw-r--r-- 1 root root 231 Oct 24 2022 .example.env drwxr-xr-x 2 root root 4096 Oct 24 2022 extend -rw-r--r-- 1 root root 34 Oct 24 2022 .gitignore -rw-r--r-- 1 root root 1822 Oct 24 2022 LICENSE.txt drwxr-xr-x 3 root root 4096 Oct 24 2022 public -rw-r--r-- 1 root root 1459 Oct 24 2022 README.md drwxr-xr-x 2 root root 4096 Oct 24 2022 route drwxr-xr-x 2 root root 4096 Oct 24 2022 runtime -rw-r--r-- 1 root root 180 Oct 24 2022 think -rw-r--r-- 1 root root 2038 Oct 24 2022 .travis.yml drwxr-xr-x 8 root root 4096 Nov 14 17:05 vendor drwxr-xr-x 2 root root 4096 Oct 24 2022 view [root@yfw wwwroot]#
11-15
502 Bad Gateway nginx [root@yfw ~]# cd /www/wwwroot/yfw.szrengjing.com [root@yfw yfw.szrengjing.com]# ./deploy.sh 🔧 开始自动化部署... fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git ℹ️ Git 仓库不存在,跳过拉取 Installing dependencies from lock file Verifying lock file contents can be installed on current platform. Nothing to install, update or remove Generating optimized autoload files > @php think service:discover Succeed! > @php think vendor:publish Succeed! 2 packages you are using are looking for funding. Use the `composer fund` command to find out more! Clear Successed 💡 storage link 已存在 ⚙️ 创建 public/.user.ini 🔐 初始化管理员账户... PHP Fatal error: Uncaught InvalidArgumentException: Undefined db config:mysql in /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php:235 Stack trace: #0 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(248): think\DbManager->getConnectionConfig() #1 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(220): think\DbManager->createConnection() #2 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(203): think\DbManager->instance() #3 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(373): think\DbManager->connect() #4 [internal function]: think\DbManager->__call() #5 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/framework/src/think/Facade.php(96): call_user_func_array() #6 Standard input code(6): think\Facade::__callStatic() #7 {main} thrown in /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php on line 235 Fatal error: Uncaught InvalidArgumentException: Undefined db config:mysql in /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php:235 Stack trace: #0 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(248): think\DbManager->getConnectionConfig() #1 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(220): think\DbManager->createConnection() #2 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(203): think\DbManager->instance() #3 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php(373): think\DbManager->connect() #4 [internal function]: think\DbManager->__call() #5 /www/wwwroot/yfw.szrengjing.com/vendor/topthink/framework/src/think/Facade.php(96): call_user_func_array() #6 Standard input code(6): think\Facade::__callStatic() #7 {main} thrown in /www/wwwroot/yfw.szrengjing.com/vendor/topthink/think-orm/src/DbManager.php on line 235 🔄 重载 PHP-FPM... Job for php-fpm-53.service failed. See "systemctl status php-fpm-53.service" and "journalctl -xe" for details. 🎉 部署完成!访问后台: http://yfw.szrengjing.com [root@yfw yfw.szrengjing.com]#
11-15
当使用 Nginx 搭配 PHP-FPM 处理 PHP 请求时,可能会遇到访问 PHP 文件返回“File not found”错误。该问题通常由配置不当引起,导致服务器无法正确识别并执行 PHP 文件。 为确保 PHP 文件能够被正确解析,Nginx 配置中应包含以下关键指令: - `try_files $uri =404;` 可防止请求的文件不存在时继续传递给 PHP-FPM,而是直接返回 404 错误[^1]。 - `fastcgi_pass` 应指向运行中的 PHP-FPM 实例,可以是 Unix 套接字或 TCP 地址(如 `127.0.0.1:9000`)。 - `fastcgi_index index.php;` 用于指定默认处理的入口文件。 - `fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;` 是至关重要的参数,它告诉 PHP-FPM 当前请求的脚本路径,若未设置可能导致“File not found”错误[^3]。 - `include fastcgi_params;` 包含了标准 FastCGI 参数集合,确保环境变量传递正常。 一个典型的配置如下所示: ```nginx location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 此外,如果设置了 `root` 或 `alias` 指令,请确认其值与网站文档根目录一致,否则可能造成脚本路径解析错误。例如: ```nginx location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 完成上述修改后,需重新加载 Nginx 配置以使更改生效: ```bash nginx -s reload ``` 若问题仍未解决,建议检查 PHP-FPM 是否正常运行,并查看 Nginx 和 PHP-FPM 的日志文件,它们通常位于 `/var/log/nginx/error.log` 和 `/var/log/php-fpm.log` 中,可提供更详细的错误信息帮助定位问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值