Nginx + PHP-FPM executes script, but returns 404

Posted by MorfiusX on Server Fault See other posts from Server Fault or by MorfiusX
Published on 2012-11-24T15:08:35Z Indexed on 2012/11/24 17:05 UTC
Read the original article Hit count: 522

I am using Nginx + PHP-FPM to run a Wordpress based site. I have a URL that should return dynamically generated JSON data for use with the DataTables jQuery plugin. The data is returned properly, but with a return code of 404. I think this is a Nginx config issue, but I haven't been able to figure out why. The script 'getTable.php' works properly on the production version of the site which is currently using Apache. Anyone know how I can get this to work on Nginx?

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

SERVER: CentOS 6 + Varnish (caching disabled for development) + Nginx + PHP-FPM + Wordpress + W3 Total Cache

Nginx Config:

    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

    }

}

Fast CGI Params:

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;

UPDATE: Upon further digging, it looks like Nginx is generating the 404 and PHP-FPM is executing the script properly and returning a 200.

UPDATE: Here are the contents of the script:

    <?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);

© Server Fault or respective owner

Related posts about nginx

Related posts about Wordpress