Nginx+PHP FPM 出现 File not found 的可能性有两个:

  1. PHP FPM 找不到 SCRIPT_FILENAME 即 php 文件;
  2. 文件没有读取权限。

因为是在本地安装,权限没问题,所以问题应该是第一个。

检查了一下,发现原来是因为复制了老的 Nginx 配置导致的。

老的配置文件是这样的:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

修改成新的配置:

location ~ \.php$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

重启 nginx,问题解决。


顺便贴一下我的 nginx+php fpm 下安装 wordpress 的 macos 本地的 nginx 完整配置:

server {
    listen 80;
    server_name local.crowall.com;
    root /Users/tony/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/www;
    }

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

注意,如果 fpm 是以非端口的形式跑的,fastcgi_pass 127.0.0.1:9000; 这一行要改成对应的 sock 文件地址。

标签: wordpress, 404