Nginx + PHP-FPM 执行脚本,但返回 404

Nginx + PHP-FPM 执行脚本,但返回 404

我正在使用 Nginx + PHP-FPM 来运行基于 Wordpress 的网站。我有一个 URL,它应该返回动态生成的 JSON 数据,以供 DataTables jQuery 插件使用。数据返回正确,但返回代码为 404。我认为这是 Nginx 配置问题,但我无法找出原因。脚本“getTable.php”在当前使用 Apache 的网站生产版本上正常运行。有人知道如何让它在 Nginx 上运行吗?

网址:http://dev.iloveskydiving.org/wp-content/plugins/ils-workflow/lib/getTable.php

服务器:CentOS 6 + Varnish(开发时禁用缓存)+ Nginx + PHP-FPM + Wordpress + W3 Total Cache

Nginx配置:

    server {

    # Server Parameters
    listen       127.0.0.1:8082;
    server_name  dev.iloveskydiving.org;
    root         /var/www/dev.iloveskydiving.org/html;
    access_log   /var/www/dev.iloveskydiving.org/logs/access.log main;
    error_log    /var/www/dev.iloveskydiving.org/logs/error.log error;
    index        index.php;

    # Rewrite minified CSS and JS files
    location ~* \.(css|js) {
        if (!-f $request_filename) {
            rewrite ^/wp-content/w3tc/min/(.+\.(css|js))$ /wp-content/w3tc/min/index.php?file=$1 last;
            expires         max;
        }
    }

    # Set a variable to work around the lack of nested conditionals
    set $cache_uri $request_uri;

    # Don't cache uris containing the following segments
    if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") {
        set $cache_uri "no cache";
    }

    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp\-postpass|wordpress_logged_in") {
        set $cache_uri 'no cache';
    }

    # Use cached or actual file if they exists, otherwise pass request to WordPress
    location / {
        try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?q=$uri&$args;
    }

    # Cache static files for as long as possible
    location ~* \.(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        try_files       $uri =404;
        expires         max;
        access_log      off;
    }

    # Deny access to hidden files
    location ~* /\.ht {
        deny            all;
        access_log      off;
        log_not_found   off;
    }

    location ~ \.php$ {

        try_files      $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include        /etc/nginx/fastcgi_params;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO          $fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass   unix:/var/lib/php-fpm/php-fpm.sock;  # port where FastCGI processes were spawned

    }

}

快速 CGI 参数:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

更新: 进一步挖掘后,看起来 Nginx 正在生成 404 并且 PHP-FPM 正在正确执行脚本并返回 200。

更新: 脚本内容如下:

    <?php
/**
 * Connect to Wordpres
 */
require(dirname(__FILE__) . '/../../../../wp-blog-header.php');

/**
 * Define temporary array
 */
$aaData = array();
$aaData['aaData'] = array();

/**
 * Execute Query
 */
$query = new WP_Query(
    array(
        'post_type'      => 'post',
        'posts_per_page' => '-1'
    )
);

foreach ($query->posts as $post) {

    array_push(
        $aaData['aaData'],
        array(
            $post->post_title
        )
    );

}

/**
 * Echo JSON encoded array
 */
echo json_encode($aaData);

答案1

Panagiotis Papadomitsos 的问题促使我测试了几个新的可能原因。我找到了以下文章: https://cooltrainer.org/2010/06/20/fixing-false-404-headers-on-external-pages- including-wp-blog-header-php/

我在我的代码中更改了以下行:

regquire(dirname(__FILE__) . '/../../../../wp-blog-header.php');

到:

require(dirname(__FILE__) . '/../../../../wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
//$wp->send_headers();

根本原因似乎是 Wordpress 返回 404,而不是 Nginx 或 PHP-FPM。

答案2

尝试关闭错误拦截:

fastcgi_intercept_errors on;

看看会发生什么。另外,当您请求脚本时,您能否粘贴访问和错误日​​志的相关行?

相关内容