You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx未调用PHP解析器:PHP7测试服务器配置问题排查求助

排查Nginx下PHP文件以纯文本返回的问题

让我帮你梳理下这个PHP文件未被解析的问题,从你贴出的Nginx配置片段来看,有几个关键的配置错误很可能是罪魁祸首:

1. SCRIPT_FILENAME路径与root不匹配

你在location ~ \.php$里设置的root是/var/www/site,但SCRIPT_FILENAME却写了/var/www/mygagenet$fastcgi_script_name——这两个路径完全不一致!PHP-FPM找不到对应的脚本文件,就会导致Nginx直接把原文件返回给客户端。

修复方案:把SCRIPT_FILENAME改成和root匹配的路径,推荐使用$document_root变量(它会自动继承当前location或server块的root配置),这样更灵活:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

如果要写死路径,就改成/var/www/site$fastcgi_script_name。

2. fastcgi_params的包含顺序错误

你把include /etc/nginx/fastcgi_params;放在了自定义的fastcgi_param之后,这会直接覆盖你自定义的参数!因为fastcgi_params文件里默认已经包含了SCRIPT_FILENAME的配置,后加载的配置会覆盖之前的,导致你设置的正确路径根本没生效。

修复方案:先加载默认的fastcgi_params,再设置自定义的参数:

# 先包含默认参数
include /etc/nginx/fastcgi_params;
# 再设置自定义参数,确保不会被覆盖
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/my.site.net_error.log";

3. 可选优化:调整root的位置

把root放到server块下,让所有location都继承这个配置,避免在多个location里重复设置,也能减少路径不一致的问题:

server {
    listen 443 ssl spdy;
    server_name local.site.com xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx eth2.site.com "";
    # 把root移到server块
    root /var/www/site;

    location ~ \.php$ {
        # 这里就不需要再写root了
        fastcgi_pass unix:/usr/local/phpbrew/php/php-7.1.13/var/run/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/my.site.net_error.log";
    }
}

修复后的验证步骤

  1. 先检查Nginx配置语法是否正确:
nginx -t
  1. 如果语法没问题,重载Nginx使配置生效:
# 不同系统可能命令不同,选一个适合你的
systemctl reload nginx
# 或者
service nginx reload
  1. 查看PHP-FPM的日志(一般在/usr/local/phpbrew/php/php-7.1.13/var/log/目录下),确认是否有脚本文件找不到的错误,这能帮你进一步排查路径问题。
  2. 最后再用浏览器或curl访问PHP文件,确认是否已经正常解析。

内容的提问来源于stack exchange,提问作者tracer tong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:38