文件未找到 nginx php-fpm。

15

我已经查看了所有类似的问题,并尝试应用上面提到的修复措施,但都没有成功。

我正在使用 wordpress:4.7.3-php7.0-fpm-alpine Docker镜像,并在其前面使用单独的nginx容器。

当我使用curl访问WordPress时,我得到以下结果:

File not found.

当我检查 WordPress 容器的日志时,我得到:

127.0.0.1 -  16/Mar/2017:06:26:24 +0000 "GET /index.php" 404
127.0.0.1 -  16/Mar/2017:06:31:27 +0000 "GET /index.php" 404
127.0.0.1 -  16/Mar/2017:06:32:16 +0000 "GET /index.php" 404
127.0.0.1 -  16/Mar/2017:06:37:17 +0000 "GET /index.php" 404
127.0.0.1 -  16/Mar/2017:06:39:09 +0000 "GET /index.php" 404

实际的 nginx 错误是:

2017/03/16 06:26:24 [error] 17#17: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.128.0.7, server: k8wp, request
: "GET / HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000"

我正在使用php 7

/var/www/html # php-fpm -v
PHP 7.0.16 (fpm-fcgi) (built: Mar  3 2017 23:07:56)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies

我的nginx配置如下:

server {
    root /app;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    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;
    }
}

我正在使用用户www-data运行nginx:

user www-data;
根据 /usr/local/etc/php-fpm.d/www.conf,用户和组已取消注释并设置为www-data。 答案:根据 /usr/local/etc/php-fpm.d/www.conf,用户和组已取消注释并设置为www-data

你想要执行的实际文件路径是什么? - Sahil Gulati
@SahilGulati 在 WordPress 容器中,它位于 /var/www/html/index.php,在 Nginx 容器中,它位于 /app。 - Jonathan
刚刚看到一篇文章指出,从消息的格式来看,这是一个 PHP 错误而不是 Nginx 错误。 - Jonathan
你在WordPress下使用MySQL吗? - lvthillo
4个回答

27
错误提示表明你的SCRIPT_FILENAME不正确。你的评论:

在 WordPress 容器中,它位于 /var/www/html/index.php,在 Nginx 容器中,它位于 /app

暗示着nginxphp-fpm正在看到不同的文档根目录。
如果是这种情况,请使用:
fastcgi_param   SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;

2
你可以尝试包含include snippets/fastcgi-php.conf;。这应该可以解决你的问题。

1
我遇到的问题是我的WordPress安装在子目录中。因此,/blog/ WordPress的主索引会加载,但是深层目录中的已发布博客条目或页面(例如/blog/wp-admin/)都无法加载。
为了解决这个问题,我添加了下面这个块:
location /blog {
                try_files $uri $uri/ /blog/index.php?$args;
        }

这实际上在nginx wordpress配方页面的"位置策略"中有解释。


0

我的问题是由于将网站文件夹放在用户主目录中而导致的。

一旦我将其从/home/ubuntu/my-website移动到/srv/my-website,一切都正常了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接