运维工程师技术之nginx搭配php
2026/5/11 14:16:01 网站建设 项目流程

出现“File not found.”的核心原因是fastcgi_param SCRIPT_FILENAME配置错误,导致PHP-FPM找不到对应的PHP文件。以下是具体分析和修复方案:

一、直接原因:SCRIPT_FILENAME路径错误

你配置中的这一行:

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

是Nginx默认的错误写法——/scripts是一个不存在的目录,而你的PHP文件实际在/app/code/zbx目录下。

当Nginx把请求转发给PHP-FPM时,会告诉它“要执行的文件在/scripts/xxx.php”,但这个路径根本不存在,所以PHP-FPM返回“File not found.”。

二、修复步骤

1. 修正SCRIPT_FILENAME参数

/scripts改为$document_root(自动关联当前配置的root目录),同时补充roottry_files指令(确保路径正确):

location ~ \.php$ { root /app/code/zbx; # 必须和上面location /的root一致,指定PHP文件所在目录 try_files $uri =404; # 先检查文件是否存在,不存在直接返回404(避免无效请求) fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # 关键修正:用$document_root获取当前root目录,正确指向PHP文件 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
2. 补充index的优先级(可选但建议)

index中的index.php放到前面,优先解析PHP索引页:

location / { root /app/code/zbx; index index.php index.htm; # 把index.php放前面 }

三、修复后的完整配置

server { listen 80; server_name localhost www.zbx888.com; location / { root /app/code/zbx; index index.php index.htm; # 优先解析PHP索引页 try_files $uri $uri/ /index.php$is_args$args; # 处理PHP项目路由 } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # 修复后的PHP解析块 location ~ \.php$ { root /app/code/zbx; # 和项目根目录一致 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; } # 禁止访问隐藏文件(提升安全性) location ~ /\.ht { deny all; } }

四、验证与生效

  1. 检查配置语法

    nginx -t

    输出“test is successful”说明语法正确。

  2. 重启Nginx

    systemctl restart nginx
  3. 确认PHP文件存在
    确保/app/code/zbx目录下有index.php(或你访问的PHP文件):

    ls-l /app/code/zbx/index.php

五、额外排查(若仍报错)

如果修复后还是“File not found.”,检查:

  • PHP-FPM的用户权限:PHP-FPM默认以apachenginx用户运行,需确保该用户能读取/app/code/zbx下的文件:
    chown-R nginx:nginx /app/code/zbxchmod-R755/app/code/zbx
  • SELinux限制:临时关闭SELinux测试(生产环境需配置规则):
    setenforce0

修复后,浏览器访问www.zbx888.com就能正常解析PHP文件了。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询